public static byte[] derive(String P, String S, int c, int dkLen) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { int hLen = 20; if (dkLen > ((Math.pow(2, 32)) - 1) * hLen) { throw new IllegalArgumentException("derived key too long"); } else { int l = (int) Math.ceil((double) dkLen / (double) hLen); // int r = dkLen - (l-1)*hLen; for (int i = 1; i <= l; i++) { byte[] T = F(P, S, c, i); baos.write(T); } } } catch (Exception e) { throw new RuntimeException(e); } byte[] baDerived = new byte[dkLen]; System.arraycopy(baos.toByteArray(), 0, baDerived, 0, baDerived.length); return baDerived; }
/** * Convert mnemonic word list to seed. */ public static byte[] toSeed(List<String> words, String passphrase) { // To create binary seed from mnemonic, we use PBKDF2 function // with mnemonic sentence (in UTF-8) used as a password and // string "mnemonic" + passphrase (again in UTF-8) used as a // salt. Iteration count is set to 4096 and HMAC-SHA512 is // used as a pseudo-random function. Desired length of the // derived key is 512 bits (= 64 bytes). // String pass = Utils.SPACE_JOINER.join(words); String salt = "mnemonic" + passphrase; final Stopwatch watch = Stopwatch.createStarted(); byte[] seed = PBKDF2SHA512.derive(pass, salt, PBKDF2_ROUNDS, 64); watch.stop(); log.info("PBKDF2 took {}", watch); return seed; }
private static byte[] F(String P, String S, int c, int i) throws Exception { byte[] U_LAST = null; byte[] U_XOR = null; SecretKeySpec key = new SecretKeySpec(P.getBytes("UTF-8"), "HmacSHA512"); Mac mac = Mac.getInstance(key.getAlgorithm()); mac.init(key); for (int j = 0; j < c; j++) { if (j == 0) { byte[] baS = S.getBytes("UTF-8"); byte[] baI = INT(i); byte[] baU = new byte[baS.length + baI.length]; System.arraycopy(baS, 0, baU, 0, baS.length); System.arraycopy(baI, 0, baU, baS.length, baI.length); U_XOR = mac.doFinal(baU); U_LAST = U_XOR; mac.reset(); } else { byte[] baU = mac.doFinal(U_LAST); mac.reset(); for (int k = 0; k < U_XOR.length; k++) { U_XOR[k] = (byte) (U_XOR[k] ^ baU[k]); } U_LAST = baU; } } return U_XOR; }
private static byte[] F(String P, String S, int c, int i) throws Exception { byte[] U_LAST = null; byte[] U_XOR = null; SecretKeySpec key = new SecretKeySpec(P.getBytes("UTF-8"), "HmacSHA512"); Mac mac = Mac.getInstance(key.getAlgorithm()); mac.init(key); for (int j = 0; j < c; j++) { if (j == 0) { byte[] baS = S.getBytes("UTF-8"); byte[] baI = INT(i); byte[] baU = new byte[baS.length + baI.length]; System.arraycopy(baS, 0, baU, 0, baS.length); System.arraycopy(baI, 0, baU, baS.length, baI.length); U_XOR = mac.doFinal(baU); U_LAST = U_XOR; mac.reset(); } else { byte[] baU = mac.doFinal(U_LAST); mac.reset(); for (int k = 0; k < U_XOR.length; k++) { U_XOR[k] = (byte) (U_XOR[k] ^ baU[k]); } U_LAST = baU; } } return U_XOR; }
/** * Convert mnemonic word list to seed. */ public static byte[] toSeed(List<String> words, String passphrase) { // To create binary seed from mnemonic, we use PBKDF2 function // with mnemonic sentence (in UTF-8) used as a password and // string "mnemonic" + passphrase (again in UTF-8) used as a // salt. Iteration count is set to 4096 and HMAC-SHA512 is // used as a pseudo-random function. Desired length of the // derived key is 512 bits (= 64 bytes). // String pass = Utils.join(words); String salt = "mnemonic" + passphrase; final Stopwatch watch = Stopwatch.createStarted(); byte[] seed = PBKDF2SHA512.derive(pass, salt, PBKDF2_ROUNDS, 64); watch.stop(); log.info("PBKDF2 took {}", watch); return seed; }
public static byte[] derive(String P, String S, int c, int dkLen) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { int hLen = 20; if (dkLen > ((Math.pow(2, 32)) - 1) * hLen) { throw new IllegalArgumentException("derived key too long"); } else { int l = (int) Math.ceil((double) dkLen / (double) hLen); // int r = dkLen - (l-1)*hLen; for (int i = 1; i <= l; i++) { byte[] T = F(P, S, c, i); baos.write(T); } } } catch (Exception e) { throw new RuntimeException(e); } byte[] baDerived = new byte[dkLen]; System.arraycopy(baos.toByteArray(), 0, baDerived, 0, baDerived.length); return baDerived; }
private static byte[] F(String P, String S, int c, int i) throws Exception { byte[] U_LAST = null; byte[] U_XOR = null; SecretKeySpec key = new SecretKeySpec(P.getBytes("UTF-8"), "HmacSHA512"); Mac mac = Mac.getInstance(key.getAlgorithm()); mac.init(key); for (int j = 0; j < c; j++) { if (j == 0) { byte[] baS = S.getBytes("UTF-8"); byte[] baI = INT(i); byte[] baU = new byte[baS.length + baI.length]; System.arraycopy(baS, 0, baU, 0, baS.length); System.arraycopy(baI, 0, baU, baS.length, baI.length); U_XOR = mac.doFinal(baU); U_LAST = U_XOR; mac.reset(); } else { byte[] baU = mac.doFinal(U_LAST); mac.reset(); for (int k = 0; k < U_XOR.length; k++) { U_XOR[k] = (byte) (U_XOR[k] ^ baU[k]); } U_LAST = baU; } } return U_XOR; }
/** * Convert mnemonic word list to seed. */ public static byte[] toSeed(List<String> words, String passphrase) { // To create binary seed from mnemonic, we use PBKDF2 function // with mnemonic sentence (in UTF-8) used as a password and // string "mnemonic" + passphrase (again in UTF-8) used as a // salt. Iteration count is set to 4096 and HMAC-SHA512 is // used as a pseudo-random function. Desired length of the // derived key is 512 bits (= 64 bytes). // String pass = Utils.join(words); String salt = "mnemonic" + passphrase; final Stopwatch watch = Stopwatch.createStarted(); byte[] seed = PBKDF2SHA512.derive(pass, salt, PBKDF2_ROUNDS, 64); watch.stop(); log.info("PBKDF2 took {}", watch); return seed; }
public static byte[] derive(String P, String S, int c, int dkLen) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { int hLen = 20; if (dkLen > ((Math.pow(2, 32)) - 1) * hLen) { throw new IllegalArgumentException("derived key too long"); } else { int l = (int) Math.ceil((double) dkLen / (double) hLen); // int r = dkLen - (l-1)*hLen; for (int i = 1; i <= l; i++) { byte[] T = F(P, S, c, i); baos.write(T); } } } catch (Exception e) { throw new RuntimeException(e); } byte[] baDerived = new byte[dkLen]; System.arraycopy(baos.toByteArray(), 0, baDerived, 0, baDerived.length); return baDerived; }
private static byte[] F(String P, String S, int c, int i) throws Exception { byte[] U_LAST = null; byte[] U_XOR = null; SecretKeySpec key = new SecretKeySpec(P.getBytes("UTF-8"), "HmacSHA512"); Mac mac = Mac.getInstance(key.getAlgorithm()); mac.init(key); for (int j = 0; j < c; j++) { if (j == 0) { byte[] baS = S.getBytes("UTF-8"); byte[] baI = INT(i); byte[] baU = new byte[baS.length + baI.length]; System.arraycopy(baS, 0, baU, 0, baS.length); System.arraycopy(baI, 0, baU, baS.length, baI.length); U_XOR = mac.doFinal(baU); U_LAST = U_XOR; mac.reset(); } else { byte[] baU = mac.doFinal(U_LAST); mac.reset(); for (int k = 0; k < U_XOR.length; k++) { U_XOR[k] = (byte) (U_XOR[k] ^ baU[k]); } U_LAST = baU; } } return U_XOR; }
/** * Convert mnemonic word list to seed. */ public static byte[] toSeed(List<String> words, String passphrase) { checkNotNull(passphrase, "A null passphrase is not allowed."); // To create binary seed from mnemonic, we use PBKDF2 function // with mnemonic sentence (in UTF-8) used as a password and // string "mnemonic" + passphrase (again in UTF-8) used as a // salt. Iteration count is set to 4096 and HMAC-SHA512 is // used as a pseudo-random function. Desired length of the // derived key is 512 bits (= 64 bytes). // String pass = Utils.join(words); String salt = "mnemonic" + passphrase; final Stopwatch watch = Stopwatch.createStarted(); byte[] seed = PBKDF2SHA512.derive(pass, salt, PBKDF2_ROUNDS, 64); watch.stop(); log.info("PBKDF2 took {}", watch); return seed; }
public static byte[] derive(String P, String S, int c, int dkLen) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { int hLen = 20; if (dkLen > ((Math.pow(2, 32)) - 1) * hLen) { throw new IllegalArgumentException("derived key too long"); } else { int l = (int) Math.ceil((double) dkLen / (double) hLen); // int r = dkLen - (l-1)*hLen; for (int i = 1; i <= l; i++) { byte[] T = F(P, S, c, i); baos.write(T); } } } catch (Exception e) { throw new RuntimeException(e); } byte[] baDerived = new byte[dkLen]; System.arraycopy(baos.toByteArray(), 0, baDerived, 0, baDerived.length); return baDerived; }