Skip to content

Commit

Permalink
More Crypto tiding
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Aug 16, 2024
1 parent 9e6aace commit c636af4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 68 deletions.
8 changes: 4 additions & 4 deletions convex-core/src/main/java/convex/core/crypto/AKeyPair.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ public static AKeyPair create(byte[] keyMaterial) {
}

/**
* Create a key pair with the given seed. Public key is generated
* Create a key pair with the given Ed25519 seed. Public key is generated
* automatically from the private key
*
* @param seed 32 bytes of seed material
* @param ed25519seed 32 bytes of seed material
* @return A new key pair using the given seed
*/
public static AKeyPair create(Blob seed) {
return Providers.generate(seed);
public static AKeyPair create(Blob ed25519seed) {
return Providers.generate(ed25519seed);
}

/**
Expand Down
11 changes: 6 additions & 5 deletions convex-core/src/main/java/convex/core/crypto/BIP39.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public static AKeyPair seedToKeyPair(Blob seed) {
}

/**
* Converts a BIP39 seed to an Ed25519 seed. This is defined as the SHA3-256 hash of the BIP39 seed
* Converts a BIP39 seed to an Ed25519 seed. This is done by taking the first 32 bytes of the SLIP-10 master key
*
* Note: longer term users may want hierarchical deterministic wallet generation
*
Expand Down Expand Up @@ -302,11 +302,12 @@ private static Blob getSeedInternal(char[] normalisedMnemonic, String passphrase
throw new Error("Security error getting BIP39 seed",e);
}
}

public static String createSecureMnemonic() {
return createSecureMnemonic(12);
}

/**
* Creates a normalised BIP39 mnemonic with the specified number of words
* @param numWords
* @return
*/
public static String createSecureMnemonic(int numWords) {
return Utils.joinStrings(createWords(new SecureRandom(),numWords)," ");
}
Expand Down
57 changes: 0 additions & 57 deletions convex-core/src/main/java/convex/core/crypto/PEMTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
import java.io.StringReader;
import java.io.StringWriter;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;

import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.openssl.PEMParser;
Expand All @@ -32,51 +28,6 @@ public class PEMTools {
Providers.init();
}

/**
* Writes a key pair to a String
* @param kp Key pair to write
* @return PEM String representation of key pair
*/
public static String writePEM(AKeyPair kp) {

PrivateKey priv=kp.getPrivate();
// PublicKey pub=kp.getPublic();
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(priv.getEncoded());

byte[] encoded=keySpec.getEncoded();
String base64=Base64.getEncoder().encodeToString(encoded);

StringBuilder sb=new StringBuilder();
sb.append("-----BEGIN PRIVATE KEY-----");
sb.append(System.lineSeparator());
sb.append(base64);
sb.append(System.lineSeparator());
sb.append("-----END PRIVATE KEY-----");
String pem=sb.toString();
return pem;
}

/**
* Read a key pair from a PEM String
* @param pem PEM String
* @return Key pair instance
* @throws GeneralSecurityException If a security error occurs
*/
public static AKeyPair readPEM(String pem) throws GeneralSecurityException {
String publicKeyPEM = pem.trim()
.replace("-----BEGIN PRIVATE KEY-----", "")
.replaceAll(System.lineSeparator(), "")
.replace("-----END PRIVATE KEY-----", "");

byte[] bs = Base64.getDecoder().decode(publicKeyPEM);

KeyFactory keyFactory = KeyFactory.getInstance("Ed25519");
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bs);
PrivateKey priv=keyFactory.generatePrivate(keySpec);
PublicKey pub=keyFactory.generatePublic(keySpec);
return AKeyPair.create(pub, priv);
}

/**
* Encrypt a private key into a PEM formated text
*
Expand Down Expand Up @@ -145,12 +96,4 @@ public static AKeyPair decryptPrivateKeyFromPEM(String pemText, char[] password)
}
}

public static void main(String[] args) throws Exception {
AKeyPair kp=AKeyPair.createSeeded(1337);
String pem=writePEM(kp);
System.out.println(pem);

AKeyPair kp2=readPEM(pem);
System.out.println(kp2);
}
}
5 changes: 3 additions & 2 deletions convex-gui/src/main/java/convex/gui/keys/KeyGenPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private void generateBIP39Seed() {
warn +="Not in standard word list: "+badWord+". ";
}
if (p.isBlank()) {
warn+="Passphrse is blank!";
warn+="Passphrase is blank!";
} else {
int entropy=Passwords.estimateEntropy(p);
if (entropy<10) {
Expand All @@ -111,7 +111,7 @@ private void generateBIP39Seed() {

if (warn.isBlank()) {
warningArea.setForeground(Color.GREEN);
warningArea.setText("Looks OK");
warningArea.setText("OK: Reasonable mnemonic and passphrase");
} else {
warningArea.setForeground(Color.ORANGE);
warningArea.setText("WARNING: "+warn);
Expand Down Expand Up @@ -288,6 +288,7 @@ public KeyGenPanel(PeerGUI manager) {
warningArea.setLineWrap(true);
warningArea.setWrapStyleWord(true);
warningArea.setEditable(false);
warningArea.setToolTipText("This is a quick heuristic check of mnemonic and passphrase.\nHeeding any warnings is advised, but you can ignore them if you know what you are doing (or don't care).");
formPanel.add(warningArea,TEXTAREA_CONSTRAINT);
}

Expand Down

0 comments on commit c636af4

Please sign in to comment.