minor additions to support bip129

This commit is contained in:
Craig Raw 2023-02-22 10:19:02 +02:00
parent 50fdb71bf3
commit caed93ca6d
2 changed files with 17 additions and 1 deletions

View file

@ -23,6 +23,7 @@ public class Utils {
public static final String HEX_REGEX = "^[0-9A-Fa-f]+$"; public static final String HEX_REGEX = "^[0-9A-Fa-f]+$";
public static final String BASE64_REGEX = "^[0-9A-Za-z\\\\+=/]+$"; public static final String BASE64_REGEX = "^[0-9A-Za-z\\\\+=/]+$";
public static final String NUMERIC_REGEX = "^-?\\d+(\\.\\d+)?$";
public static boolean isHex(String s) { public static boolean isHex(String s) {
return s.matches(HEX_REGEX); return s.matches(HEX_REGEX);
@ -32,6 +33,10 @@ public class Utils {
return s.matches(BASE64_REGEX); return s.matches(BASE64_REGEX);
} }
public static boolean isNumber(String s) {
return s.matches(NUMERIC_REGEX);
}
public static boolean isUtf8(byte[] bytes) { public static boolean isUtf8(byte[] bytes) {
try { try {
CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder(); CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();

View file

@ -7,25 +7,36 @@ import org.bouncycastle.crypto.params.KeyParameter;
public class Pbkdf2KeyDeriver implements KeyDeriver, AsymmetricKeyDeriver { public class Pbkdf2KeyDeriver implements KeyDeriver, AsymmetricKeyDeriver {
public static final int DEFAULT_ITERATION_COUNT = 1024; public static final int DEFAULT_ITERATION_COUNT = 1024;
public static final int DEFAULT_KEY_SIZE = 512;
private final byte[] salt; private final byte[] salt;
private final int iterationCount; private final int iterationCount;
private final int keySize;
public static final Pbkdf2KeyDeriver DEFAULT_INSTANCE = new Pbkdf2KeyDeriver(); public static final Pbkdf2KeyDeriver DEFAULT_INSTANCE = new Pbkdf2KeyDeriver();
public Pbkdf2KeyDeriver() { public Pbkdf2KeyDeriver() {
this.salt = new byte[0]; this.salt = new byte[0];
this.iterationCount = DEFAULT_ITERATION_COUNT; this.iterationCount = DEFAULT_ITERATION_COUNT;
this.keySize = DEFAULT_KEY_SIZE;
} }
public Pbkdf2KeyDeriver(byte[] salt) { public Pbkdf2KeyDeriver(byte[] salt) {
this.salt = salt; this.salt = salt;
this.iterationCount = DEFAULT_ITERATION_COUNT; this.iterationCount = DEFAULT_ITERATION_COUNT;
this.keySize = DEFAULT_KEY_SIZE;
} }
public Pbkdf2KeyDeriver(byte[] salt, int iterationCount) { public Pbkdf2KeyDeriver(byte[] salt, int iterationCount) {
this.salt = salt; this.salt = salt;
this.iterationCount = iterationCount; this.iterationCount = iterationCount;
this.keySize = DEFAULT_KEY_SIZE;
}
public Pbkdf2KeyDeriver(byte[] salt, int iterationCount, int keySize) {
this.salt = salt;
this.iterationCount = iterationCount;
this.keySize = keySize;
} }
@Override @Override
@ -37,7 +48,7 @@ public class Pbkdf2KeyDeriver implements KeyDeriver, AsymmetricKeyDeriver {
public Key deriveKey(CharSequence password) throws KeyCrypterException { public Key deriveKey(CharSequence password) throws KeyCrypterException {
PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(new SHA512Digest()); PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(new SHA512Digest());
gen.init(SecureString.toBytesUTF8(password), salt, iterationCount); gen.init(SecureString.toBytesUTF8(password), salt, iterationCount);
byte[] keyBytes = ((KeyParameter)gen.generateDerivedParameters(512)).getKey(); byte[] keyBytes = ((KeyParameter)gen.generateDerivedParameters(keySize)).getKey();
return new Key(keyBytes, salt, getDeriverType()); return new Key(keyBytes, salt, getDeriverType());
} }