diff options
author | Ian Coleman <coleman.ian@gmail.com> | 2016-11-10 18:58:18 +1100 |
---|---|---|
committer | Ian Coleman <coleman.ian@gmail.com> | 2016-11-10 18:58:18 +1100 |
commit | 3599674db4855614d9123f2f80dcf0a37c34eb79 (patch) | |
tree | 6bcfd76aa73c71cb4b79c21455da88c51d882468 /src/js | |
parent | 1cf1bbaff575e647ef38b091a00edfa2e1ce024d (diff) | |
download | BIP39-3599674db4855614d9123f2f80dcf0a37c34eb79.tar.gz BIP39-3599674db4855614d9123f2f80dcf0a37c34eb79.tar.zst BIP39-3599674db4855614d9123f2f80dcf0a37c34eb79.zip |
Mnemonic length can be set when using raw entropy
Diffstat (limited to 'src/js')
-rw-r--r-- | src/js/index.js | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/src/js/index.js b/src/js/index.js index 45f378d..66585a9 100644 --- a/src/js/index.js +++ b/src/js/index.js | |||
@@ -68,6 +68,7 @@ | |||
68 | DOM.network.on("change", networkChanged); | 68 | DOM.network.on("change", networkChanged); |
69 | DOM.useEntropy.on("change", setEntropyVisibility); | 69 | DOM.useEntropy.on("change", setEntropyVisibility); |
70 | DOM.entropy.on("input", delayedEntropyChanged); | 70 | DOM.entropy.on("input", delayedEntropyChanged); |
71 | DOM.entropyMnemonicLength.on("change", entropyChanged); | ||
71 | DOM.phrase.on("input", delayedPhraseChanged); | 72 | DOM.phrase.on("input", delayedPhraseChanged); |
72 | DOM.passphrase.on("input", delayedPhraseChanged); | 73 | DOM.passphrase.on("input", delayedPhraseChanged); |
73 | DOM.generate.on("click", generateClicked); | 74 | DOM.generate.on("click", generateClicked); |
@@ -744,9 +745,25 @@ | |||
744 | } | 745 | } |
745 | // Show entropy details | 746 | // Show entropy details |
746 | showEntropyFeedback(entropy); | 747 | showEntropyFeedback(entropy); |
748 | // Use entropy hash if not using raw entropy | ||
749 | var bits = entropy.binaryStr; | ||
750 | var mnemonicLength = DOM.entropyMnemonicLength.val(); | ||
751 | if (mnemonicLength != "raw") { | ||
752 | // Get bits by hashing entropy with SHA256 | ||
753 | var hash = sjcl.hash.sha256.hash(entropy.cleanStr); | ||
754 | var hex = sjcl.codec.hex.fromBits(hash); | ||
755 | bits = BigInteger.parse(hex, 16).toString(2); | ||
756 | for (var i=0; i<256-bits.length; i++) { | ||
757 | bits = "0" + bits; | ||
758 | } | ||
759 | // Truncate hash to suit number of words | ||
760 | mnemonicLength = parseInt(mnemonicLength); | ||
761 | var numberOfBits = 32 * mnemonicLength / 3; | ||
762 | bits = bits.substring(0, numberOfBits); | ||
763 | } | ||
747 | // Discard trailing entropy | 764 | // Discard trailing entropy |
748 | var bitsToUse = Math.floor(entropy.binaryStr.length / 32) * 32; | 765 | var bitsToUse = Math.floor(bits.length / 32) * 32; |
749 | var binaryStr = entropy.binaryStr.substring(0, bitsToUse); | 766 | var binaryStr = bits.substring(0, bitsToUse); |
750 | // Convert entropy string to numeric array | 767 | // Convert entropy string to numeric array |
751 | var entropyArr = []; | 768 | var entropyArr = []; |
752 | for (var i=0; i<binaryStr.length / 8; i++) { | 769 | for (var i=0; i<binaryStr.length / 8; i++) { |