Create task to generate key pairs (#823)

This commit is contained in:
napoly 2024-03-14 17:12:42 +01:00 committed by GitHub
parent 697828d773
commit 82eb081089
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 25 additions and 23 deletions

View file

@ -436,6 +436,11 @@ configure(project(':core')) {
systemProperty 'jdk.attach.allowAttachSelf', true systemProperty 'jdk.attach.allowAttachSelf', true
} }
task generateKeypairs(type: JavaExec) {
mainClass = 'haveno.core.util.GenerateKeyPairs'
classpath = sourceSets.main.runtimeClasspath
}
task havenoDeps { task havenoDeps {
doLast { doLast {
// get monero binaries download url // get monero binaries download url

View file

@ -19,16 +19,6 @@ package haveno.common.crypto;
import haveno.common.util.Hex; import haveno.common.util.Hex;
import haveno.common.util.Utilities; 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.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
@ -43,10 +33,17 @@ import java.security.spec.InvalidKeySpecException;
import java.security.spec.MGF1ParameterSpec; import java.security.spec.MGF1ParameterSpec;
import java.security.spec.X509EncodedKeySpec; import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays; 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 { public class Encryption {
private static final Logger log = LoggerFactory.getLogger(Encryption.class);
public static final String ASYM_KEY_ALGO = "RSA"; public static final String ASYM_KEY_ALGO = "RSA";
private static final String ASYM_CIPHER = "RSA/ECB/OAEPWithSHA-256AndMGF1PADDING"; private static final String ASYM_CIPHER = "RSA/ECB/OAEPWithSHA-256AndMGF1PADDING";

View file

@ -12,29 +12,29 @@ import org.bitcoinj.core.Utils;
import haveno.common.crypto.Encryption; 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. * which can be used to register arbitrators on the network.
*/ */
public class GenerateKeypairs { public class GenerateKeyPairs {
public static void main(String[] args) { public static void main(String[] args) {
// generate public/private keypairs // generate public/private key-pairs
List<SecretKey> secretKeys = new ArrayList<SecretKey>(); List<SecretKey> secretKeys = new ArrayList<>();
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
secretKeys.add(Encryption.generateSecretKey(256)); secretKeys.add(Encryption.generateSecretKey(256));
} }
// print keypairs // print key-pairs
System.out.println("Private keys:"); System.out.println("Private keys:");
for (SecretKey sk : secretKeys) { for (SecretKey sk : secretKeys) {
String privKey = Utils.HEX.encode(sk.getEncoded()); String privateKey = Utils.HEX.encode(sk.getEncoded());
System.out.println(privKey); System.out.println(privateKey);
} }
System.out.println("Corresponding public keys:"); System.out.println("Corresponding public keys:");
for (SecretKey sk : secretKeys) { for (SecretKey sk : secretKeys) {
String privKey = Utils.HEX.encode(sk.getEncoded()); String privateKey = Utils.HEX.encode(sk.getEncoded());
ECKey ecKey = ECKey.fromPrivate(new BigInteger(1, Utils.HEX.decode(privKey))); ECKey ecKey = ECKey.fromPrivate(new BigInteger(1, Utils.HEX.decode(privateKey)));
String pubKey = Utils.HEX.encode(ecKey.getPubKey()); String pubKey = Utils.HEX.encode(ecKey.getPubKey());
System.out.println(pubKey); System.out.println(pubKey);
} }