public static String generateMnemonicCodesStr(){ final StringBuilder sb = new StringBuilder(); byte[] entropy = new byte[Words.TWELVE.byteLength()]; new SecureRandom().nextBytes(entropy); new MnemonicGenerator(English.INSTANCE).createMnemonic(entropy, new MnemonicGenerator.Target() { @Override public void append(CharSequence string) { sb.append(string); } }); new SecureRandom().nextBytes(entropy); return sb.toString(); }
private static int[] wordIndexes(byte[] entropy) { final int ent = entropy.length * 8; entropyLengthPreChecks(ent); final byte[] entropyWithChecksum = Arrays.copyOf(entropy, entropy.length + 1); entropyWithChecksum[entropy.length] = firstByteOfSha256(entropy); //checksum length final int cs = ent / 32; //mnemonic length final int ms = (ent + cs) / 11; //get the indexes into the word list final int[] wordIndexes = new int[ms]; for (int i = 0, wi = 0; wi < ms; i += 11, wi++) { wordIndexes[wi] = next11Bits(entropyWithChecksum, i); } return wordIndexes; }
/** * Create a mnemonic from the word list given the entropy. * * @param entropyHex 128-256 bits of hex entropy, number of bits must also be divisible by 32 * @param target Where to write the mnemonic to */ public void createMnemonic( final CharSequence entropyHex, final Target target) { final int length = entropyHex.length(); if (length % 2 != 0) throw new RuntimeException("Length of hex chars must be divisible by 2"); final byte[] entropy = new byte[length / 2]; try { for (int i = 0, j = 0; i < length; i += 2, j++) { entropy[j] = (byte) (parseHex(entropyHex.charAt(i)) << 4 | parseHex(entropyHex.charAt(i + 1))); } createMnemonic(entropy, target); } finally { Arrays.fill(entropy, (byte) 0); } }
/** * Create a mnemonic from the word list given the entropy. * * @param entropy 128-256 bits of entropy, number of bits must also be divisible by 32 * @param target Where to write the mnemonic to */ public void createMnemonic( final byte[] entropy, final Target target) { final int[] wordIndexes = wordIndexes(entropy); try { createMnemonic(wordIndexes, target); } finally { Arrays.fill(wordIndexes, 0); } }
private static void validate(final int[] wordIndexes) throws InvalidWordCountException, InvalidChecksumException { final int ms = wordIndexes.length; final int entPlusCs = ms * 11; final int ent = (entPlusCs * 32) / 33; final int cs = ent / 32; if (entPlusCs != ent + cs) throw new InvalidWordCountException(); final byte[] entropyWithChecksum = new byte[(entPlusCs + 7) / 8]; wordIndexesToEntropyWithCheckSum(wordIndexes, entropyWithChecksum); Arrays.fill(wordIndexes, 0); final byte[] entropy = Arrays.copyOf(entropyWithChecksum, entropyWithChecksum.length - 1); final byte lastByte = entropyWithChecksum[entropyWithChecksum.length - 1]; Arrays.fill(entropyWithChecksum, (byte) 0); final byte sha = firstByteOfSha256(entropy); final byte mask = maskOfFirstNBits(cs); if (((sha ^ lastByte) & mask) != 0) throw new InvalidChecksumException(); }
/** * Create a mnemonic from the word list given the entropy. * * @param entropy 128-256 bits of entropy, number of bits must also be divisible by 32 * @param target Where to write the mnemonic to */ public void createMnemonic( final byte[] entropy, final Target target) { final int[] wordIndexes = wordIndexes(entropy); try { createMnemonic(wordIndexes, target); } finally { Arrays.fill(wordIndexes, 0); } }
private static void validate(final int[] wordIndexes) throws InvalidWordCountException, InvalidChecksumException { final int ms = wordIndexes.length; final int entPlusCs = ms * 11; final int ent = (entPlusCs * 32) / 33; final int cs = ent / 32; if (entPlusCs != ent + cs) throw new InvalidWordCountException(); final byte[] entropyWithChecksum = new byte[(entPlusCs + 7) / 8]; wordIndexesToEntropyWithCheckSum(wordIndexes, entropyWithChecksum); Arrays.fill(wordIndexes, 0); final byte[] entropy = Arrays.copyOf(entropyWithChecksum, entropyWithChecksum.length - 1); final byte lastByte = entropyWithChecksum[entropyWithChecksum.length - 1]; Arrays.fill(entropyWithChecksum, (byte) 0); final byte sha = firstByteOfSha256(entropy); final byte mask = maskOfFirstNBits(cs); if (((sha ^ lastByte) & mask) != 0) throw new InvalidChecksumException(); }
public static String generateMnemonicCodesStr(){ final StringBuilder sb = new StringBuilder(); byte[] entropy = new byte[Words.TWELVE.byteLength()]; new SecureRandom().nextBytes(entropy); new MnemonicGenerator(English.INSTANCE).createMnemonic(entropy, new MnemonicGenerator.Target() { @Override public void append(CharSequence string) { sb.append(string); } }); new SecureRandom().nextBytes(entropy); return sb.toString(); }
/** * Create a mnemonic from the word list given the entropy. * * @param entropyHex 128-256 bits of hex entropy, number of bits must also be divisible by 32 * @param target Where to write the mnemonic to */ public void createMnemonic( final CharSequence entropyHex, final Target target) { final int length = entropyHex.length(); if (length % 2 == 1) throw new RuntimeException("Length of hex chars must be divisible by 2"); final byte[] entropy = new byte[length / 2]; try { for (int i = 0, j = 0; i < length; i += 2, j++) { entropy[j] = (byte) (parseHex(entropyHex.charAt(i)) << 4 | parseHex(entropyHex.charAt(i + 1))); } createMnemonic(entropy, target); } finally { Arrays.fill(entropy, (byte) 0); } }
private static int[] wordIndexes(byte[] entropy) { final int ent = entropy.length * 8; entropyLengthPreChecks(ent); final byte[] entropyWithChecksum = Arrays.copyOf(entropy, entropy.length + 1); entropyWithChecksum[entropy.length] = firstByteOfSha256(entropy); //checksum length final int cs = ent / 32; //mnemonic length final int ms = (ent + cs) / 11; //get the indexes into the word list final int[] wordIndexes = new int[ms]; for (int i = 0, wi = 0; wi < ms; i += 11, wi++) { wordIndexes[wi] = next11Bits(entropyWithChecksum, i); } return wordIndexes; }
private static String createMnemonic(byte[] f, WordList wordList) { final StringBuilder sb = new StringBuilder(); new MnemonicGenerator(wordList) .createMnemonic(f, sb::append); return sb.toString(); } }
private static String createMnemonic(String f, WordList wordList) { final StringBuilder sb = new StringBuilder(); new MnemonicGenerator(wordList) .createMnemonic(f, sb::append); return sb.toString(); }
private static String createMnemonic(byte[] f, WordList wordList) { final StringBuilder sb = new StringBuilder(); new MnemonicGenerator(wordList) .createMnemonic(f, sb::append); return sb.toString(); }
@Test public void forFinallyCodeCoverage_createMnemonicWhenTargetThrowsException() throws Exception { assertThatThrownBy( () -> new MnemonicGenerator(English.INSTANCE) .createMnemonic(repeatString(32, "f"), (s) -> { throw new OutOfMemoryError(); })) .isInstanceOf(OutOfMemoryError.class); }