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 BASE64_REGEX = "^[0-9A-Za-z\\\\+=/]+$";
public static final String NUMERIC_REGEX = "^-?\\d+(\\.\\d+)?$";
public static boolean isHex(String s) {
return s.matches(HEX_REGEX);
@ -32,6 +33,10 @@ public class Utils {
return s.matches(BASE64_REGEX);
}
public static boolean isNumber(String s) {
return s.matches(NUMERIC_REGEX);
}
public static boolean isUtf8(byte[] bytes) {
try {
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 static final int DEFAULT_ITERATION_COUNT = 1024;
public static final int DEFAULT_KEY_SIZE = 512;
private final byte[] salt;
private final int iterationCount;
private final int keySize;
public static final Pbkdf2KeyDeriver DEFAULT_INSTANCE = new Pbkdf2KeyDeriver();
public Pbkdf2KeyDeriver() {
this.salt = new byte[0];
this.iterationCount = DEFAULT_ITERATION_COUNT;
this.keySize = DEFAULT_KEY_SIZE;
}
public Pbkdf2KeyDeriver(byte[] salt) {
this.salt = salt;
this.iterationCount = DEFAULT_ITERATION_COUNT;
this.keySize = DEFAULT_KEY_SIZE;
}
public Pbkdf2KeyDeriver(byte[] salt, int iterationCount) {
this.salt = salt;
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
@ -37,7 +48,7 @@ public class Pbkdf2KeyDeriver implements KeyDeriver, AsymmetricKeyDeriver {
public Key deriveKey(CharSequence password) throws KeyCrypterException {
PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(new SHA512Digest());
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());
}