3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-14 11:15:51 +00:00

fix issue where using an empty seed or entropy value, MnemonicCode does not throw the appropriate exception

This commit is contained in:
Giannis Dzegoutanis 2014-09-29 17:14:03 +03:00 committed by Mike Hearn
parent 036f0bec27
commit 0ccb608c7e
2 changed files with 20 additions and 1 deletions

View File

@ -148,6 +148,9 @@ public class MnemonicCode {
if (words.size() % 3 > 0)
throw new MnemonicException.MnemonicLengthException("Word list size must be multiple of three words.");
if (words.size() == 0)
throw new MnemonicException.MnemonicLengthException("Word list is empty.");
// Look up all the words in the list and construct the
// concatenation of the original entropy and the checksum.
//
@ -193,7 +196,10 @@ public class MnemonicCode {
*/
public List<String> toMnemonic(byte[] entropy) throws MnemonicException.MnemonicLengthException {
if (entropy.length % 4 > 0)
throw new MnemonicException.MnemonicLengthException("entropy length not multiple of 32 bits");
throw new MnemonicException.MnemonicLengthException("Entropy length not multiple of 32 bits.");
if (entropy.length == 0)
throw new MnemonicException.MnemonicLengthException("Entropy is empty.");
// We take initial entropy of ENT bits and compute its
// checksum by taking first ENT / 32 bits of its SHA256 hash.

View File

@ -18,6 +18,7 @@
package com.google.bitcoin.crypto;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import org.junit.Before;
import org.junit.Test;
@ -199,6 +200,18 @@ public class MnemonicCodeTest {
mc.check(words);
}
@Test(expected = MnemonicException.MnemonicLengthException.class)
public void testEmptyMnemonic() throws Exception {
List<String> words = Lists.newArrayList();
mc.check(words);
}
@Test(expected = MnemonicException.MnemonicLengthException.class)
public void testEmptyEntropy() throws Exception {
byte[] entropy = new byte[]{};
mc.toMnemonic(entropy);
}
static public List<String> split(String words) {
return new ArrayList<String>(Arrays.asList(words.split("\\s+")));
}