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 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 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(); }
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(); }