diff --git a/src/main/java/com/sparrowwallet/drongo/Utils.java b/src/main/java/com/sparrowwallet/drongo/Utils.java index 4d7136f..554c279 100644 --- a/src/main/java/com/sparrowwallet/drongo/Utils.java +++ b/src/main/java/com/sparrowwallet/drongo/Utils.java @@ -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(); diff --git a/src/main/java/com/sparrowwallet/drongo/crypto/Pbkdf2KeyDeriver.java b/src/main/java/com/sparrowwallet/drongo/crypto/Pbkdf2KeyDeriver.java index 578b27b..d3e57dc 100644 --- a/src/main/java/com/sparrowwallet/drongo/crypto/Pbkdf2KeyDeriver.java +++ b/src/main/java/com/sparrowwallet/drongo/crypto/Pbkdf2KeyDeriver.java @@ -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()); }