From 82eb0810895b702ea6bf6c10a5155cc1af5bdb91 Mon Sep 17 00:00:00 2001 From: napoly Date: Thu, 14 Mar 2024 17:12:42 +0100 Subject: [PATCH] Create task to generate key pairs (#823) --- build.gradle | 5 +++++ .../java/haveno/common/crypto/Encryption.java | 21 ++++++++---------- .../haveno/core/util/GenerateKeyPairs.java} | 22 +++++++++---------- 3 files changed, 25 insertions(+), 23 deletions(-) rename core/src/{test/java/haveno/core/util/GenerateKeypairs.java => main/java/haveno/core/util/GenerateKeyPairs.java} (66%) diff --git a/build.gradle b/build.gradle index 58ac6489b9..625ac4e2c6 100644 --- a/build.gradle +++ b/build.gradle @@ -436,6 +436,11 @@ configure(project(':core')) { systemProperty 'jdk.attach.allowAttachSelf', true } + task generateKeypairs(type: JavaExec) { + mainClass = 'haveno.core.util.GenerateKeyPairs' + classpath = sourceSets.main.runtimeClasspath + } + task havenoDeps { doLast { // get monero binaries download url diff --git a/common/src/main/java/haveno/common/crypto/Encryption.java b/common/src/main/java/haveno/common/crypto/Encryption.java index d65fff3bde..93d5a527fb 100644 --- a/common/src/main/java/haveno/common/crypto/Encryption.java +++ b/common/src/main/java/haveno/common/crypto/Encryption.java @@ -19,16 +19,6 @@ package haveno.common.crypto; import haveno.common.util.Hex; import haveno.common.util.Utilities; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.crypto.Cipher; -import javax.crypto.KeyGenerator; -import javax.crypto.Mac; -import javax.crypto.SecretKey; -import javax.crypto.spec.OAEPParameterSpec; -import javax.crypto.spec.PSource; -import javax.crypto.spec.SecretKeySpec; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.security.InvalidKeyException; @@ -43,10 +33,17 @@ import java.security.spec.InvalidKeySpecException; import java.security.spec.MGF1ParameterSpec; import java.security.spec.X509EncodedKeySpec; import java.util.Arrays; +import javax.crypto.Cipher; +import javax.crypto.KeyGenerator; +import javax.crypto.Mac; +import javax.crypto.SecretKey; +import javax.crypto.spec.OAEPParameterSpec; +import javax.crypto.spec.PSource; +import javax.crypto.spec.SecretKeySpec; +import lombok.extern.slf4j.Slf4j; +@Slf4j public class Encryption { - private static final Logger log = LoggerFactory.getLogger(Encryption.class); - public static final String ASYM_KEY_ALGO = "RSA"; private static final String ASYM_CIPHER = "RSA/ECB/OAEPWithSHA-256AndMGF1PADDING"; diff --git a/core/src/test/java/haveno/core/util/GenerateKeypairs.java b/core/src/main/java/haveno/core/util/GenerateKeyPairs.java similarity index 66% rename from core/src/test/java/haveno/core/util/GenerateKeypairs.java rename to core/src/main/java/haveno/core/util/GenerateKeyPairs.java index ce36afc586..94130b1b12 100644 --- a/core/src/test/java/haveno/core/util/GenerateKeypairs.java +++ b/core/src/main/java/haveno/core/util/GenerateKeyPairs.java @@ -12,29 +12,29 @@ import org.bitcoinj.core.Utils; import haveno.common.crypto.Encryption; /** - * This utility generates and prints public/private keypairs + * This utility generates and prints public/private key-pairs * which can be used to register arbitrators on the network. */ -public class GenerateKeypairs { - +public class GenerateKeyPairs { + public static void main(String[] args) { - - // generate public/private keypairs - List secretKeys = new ArrayList(); + + // generate public/private key-pairs + List secretKeys = new ArrayList<>(); for (int i = 0; i < 20; i++) { secretKeys.add(Encryption.generateSecretKey(256)); } - // print keypairs + // print key-pairs System.out.println("Private keys:"); for (SecretKey sk : secretKeys) { - String privKey = Utils.HEX.encode(sk.getEncoded()); - System.out.println(privKey); + String privateKey = Utils.HEX.encode(sk.getEncoded()); + System.out.println(privateKey); } System.out.println("Corresponding public keys:"); for (SecretKey sk : secretKeys) { - String privKey = Utils.HEX.encode(sk.getEncoded()); - ECKey ecKey = ECKey.fromPrivate(new BigInteger(1, Utils.HEX.decode(privKey))); + String privateKey = Utils.HEX.encode(sk.getEncoded()); + ECKey ecKey = ECKey.fromPrivate(new BigInteger(1, Utils.HEX.decode(privateKey))); String pubKey = Utils.HEX.encode(ecKey.getPubKey()); System.out.println(pubKey); }