/** * Check that the supplied mnemonic fits the BIP0039 spec. * <p> * The purpose of this method overload is to avoid constructing a mnemonic String if you have gathered a list of * words from the user. * * @param mnemonic The memorable list of words * @throws InvalidChecksumException If the last bytes don't match the expected last bytes * @throws InvalidWordCountException If the number of words is not a multiple of 3, 24 or fewer * @throws WordNotFoundException If a word in the mnemonic is not present in the word list * @throws UnexpectedWhiteSpaceException Occurs if one of the supplied words is empty */ public void validate(final Collection<? extends CharSequence> mnemonic) throws InvalidChecksumException, InvalidWordCountException, WordNotFoundException, UnexpectedWhiteSpaceException { final int[] wordIndexes = findWordIndexes(mnemonic); try { validate(wordIndexes); } finally { Arrays.fill(wordIndexes, 0); } }
/** * Get a Mnemonic validator for the given word list. * No normalization is currently performed, this is an open issue: https://github.com/NovaCrypto/BIP39/issues/13 * * @param wordList A WordList implementation * @return A validator */ public static MnemonicValidator ofWordList(final WordList wordList) { return new MnemonicValidator(wordList); }
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(); }
case ValidateWholeString: { MnemonicValidator .ofWordList(wordList) .validate(validateMode == ValidateMode.EXPECTING_BAD_WORD ? mnemonic : preventToString(mnemonic)); .collect(Collectors.toList()); MnemonicValidator .ofWordList(wordList) .validate(words); break;
/** * Check that the supplied mnemonic fits the BIP0039 spec. * * @param mnemonic The memorable list of words * @throws InvalidChecksumException If the last bytes don't match the expected last bytes * @throws InvalidWordCountException If the number of words is not a multiple of 3, 24 or fewer * @throws WordNotFoundException If a word in the mnemonic is not present in the word list * @throws UnexpectedWhiteSpaceException Occurs if one of the supplied words is empty, e.g. a double space */ public void validate(final CharSequence mnemonic) throws InvalidChecksumException, InvalidWordCountException, WordNotFoundException, UnexpectedWhiteSpaceException { validate(charSequenceSplitter.split(mnemonic)); }
private int[] findWordIndexes(final Collection<? extends CharSequence> split) throws UnexpectedWhiteSpaceException, WordNotFoundException { final int ms = split.size(); final int[] result = new int[ms]; int i = 0; for (final CharSequence buffer : split) { if (buffer.length() == 0) { throw new UnexpectedWhiteSpaceException(); } result[i++] = findWordIndex(buffer); } return result; }
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(); }
/** * Check that the supplied mnemonic fits the BIP0039 spec. * * @param mnemonic The memorable list of words * @throws InvalidChecksumException If the last bytes don't match the expected last bytes * @throws InvalidWordCountException If the number of words is not a multiple of 3, 24 or fewer * @throws WordNotFoundException If a word in the mnemonic is not present in the word list * @throws UnexpectedWhiteSpaceException Occurs if one of the supplied words is empty, e.g. a double space */ public void validate(final CharSequence mnemonic) throws InvalidChecksumException, InvalidWordCountException, WordNotFoundException, UnexpectedWhiteSpaceException { validate(charSequenceSplitter.split(mnemonic)); }
private int[] findWordIndexes(final Collection<? extends CharSequence> split) throws UnexpectedWhiteSpaceException, WordNotFoundException { final int ms = split.size(); final int[] result = new int[ms]; int i = 0; for (final CharSequence buffer : split) { if (buffer.length() == 0) { throw new UnexpectedWhiteSpaceException(); } result[i++] = findWordIndex(buffer); } return result; }
/** * Check that the supplied mnemonic fits the BIP0039 spec. * <p> * The purpose of this method overload is to avoid constructing a mnemonic String if you have gathered a list of * words from the user. * * @param mnemonic The memorable list of words * @throws InvalidChecksumException If the last bytes don't match the expected last bytes * @throws InvalidWordCountException If the number of words is not a multiple of 3, 24 or fewer * @throws WordNotFoundException If a word in the mnemonic is not present in the word list * @throws UnexpectedWhiteSpaceException Occurs if one of the supplied words is empty */ public void validate(final Collection<? extends CharSequence> mnemonic) throws InvalidChecksumException, InvalidWordCountException, WordNotFoundException, UnexpectedWhiteSpaceException { final int[] wordIndexes = findWordIndexes(mnemonic); try { validate(wordIndexes); } finally { Arrays.fill(wordIndexes, 0); } }
/** * Get a Mnemonic validator for the given word list. * No normalization is currently performed, this is an open issue: https://github.com/NovaCrypto/BIP39/issues/13 * * @param wordList A WordList implementation * @return A validator */ public static MnemonicValidator ofWordList(final WordList wordList) { return new MnemonicValidator(wordList); }