]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/BIP39.git/commitdiff
Entropy is left-padded to 256 bits
authorIan Coleman <coleman.ian@gmail.com>
Thu, 12 Jan 2017 02:47:09 +0000 (13:47 +1100)
committerIan Coleman <coleman.ian@gmail.com>
Thu, 12 Jan 2017 02:47:09 +0000 (13:47 +1100)
Closes #49

bip39-standalone.html
src/js/index.js
tests.js

index afef084e9264e880e2291807f148b7525cf021e9..3b4f0f27de389dab22a7e9812380e3754fc69abd 100644 (file)
@@ -19488,7 +19488,7 @@ window.Entropy = new (function() {
             var hash = sjcl.hash.sha256.hash(entropy.cleanStr);
             var hex = sjcl.codec.hex.fromBits(hash);
             bits = BigInteger.parse(hex, 16).toString(2);
-            for (var i=0; i<256-bits.length; i++) {
+            while (bits.length % 256 != 0) {
                 bits = "0" + bits;
             }
             // Truncate hash to suit number of words
index 531880084d71139bb3aeae33177a16c3f32f9d1b..c3c0a4aae4da5bddedde6543a668659a8516139a 100644 (file)
             var hash = sjcl.hash.sha256.hash(entropy.cleanStr);
             var hex = sjcl.codec.hex.fromBits(hash);
             bits = BigInteger.parse(hex, 16).toString(2);
-            for (var i=0; i<256-bits.length; i++) {
+            while (bits.length % 256 != 0) {
                 bits = "0" + bits;
             }
             // Truncate hash to suit number of words
index 16f19630bfa3eab9622dc91619688b187b12b952..a3c3f57956e3e810498fb22baa51a57a65687557 100644 (file)
--- a/tests.js
+++ b/tests.js
@@ -3335,6 +3335,35 @@ page.open(url, function(status) {
 });
 },
 
+// Github issue 49
+// padding for binary should give length with multiple of 256
+// hashed entropy 1111 is length 252, so requires 4 leading zeros
+// prior to issue 49 it would only generate 2 leading zeros, ie missing 2
+function() {
+page.open(url, function(status) {
+    expected = "avocado valid quantum cross link predict excuse edit street able flame large galaxy ginger nuclear"
+    // use entropy
+    page.evaluate(function() {
+        $(".use-entropy").prop("checked", true).trigger("change");
+        $(".entropy").val("1111").trigger("input");
+    });
+    waitForGenerate(function() {
+        // get the mnemonic
+        var actual = page.evaluate(function() {
+            return $(".phrase").val();
+        });
+        // check the mnemonic is correct
+        if (actual != expected) {
+            console.log("Left padding error for entropy");
+            console.log("Expected: " + expected);
+            console.log("Actual: " + actual);
+            fail();
+        }
+        next();
+    });
+});
+},
+
 // If you wish to add more tests, do so here...
 
 // Here is a blank test template