From: Ian Coleman Date: Thu, 12 Jan 2017 02:47:09 +0000 (+1100) Subject: Entropy is left-padded to 256 bits X-Git-Url: https://git.immae.eu/?a=commitdiff_plain;h=53aaab270d7031a05ffe66f424529ea84534fb40;hp=40892aba5013cd75927f63e66492f46b2d206ec9;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FBIP39.git Entropy is left-padded to 256 bits Closes #49 --- diff --git a/bip39-standalone.html b/bip39-standalone.html index afef084..3b4f0f2 100644 --- a/bip39-standalone.html +++ b/bip39-standalone.html @@ -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 diff --git a/src/js/index.js b/src/js/index.js index 5318800..c3c0a4a 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -843,7 +843,7 @@ 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 diff --git a/tests.js b/tests.js index 16f1963..a3c3f57 100644 --- 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