X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2Fjs%2Findex.js;h=3690de1c6cdbde5bca14d5459542cc24ee8b3ecb;hb=18abe53b7a5f6caa28547d8f9f93a67f8cafe5d1;hp=45db7b139861814e603903c68c5a583c5dd4fa6e;hpb=88df3739e7e8b26b461a73fe3874b195e5b03fec;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FBIP39.git diff --git a/src/js/index.js b/src/js/index.js index 45db7b1..3690de1 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -24,7 +24,14 @@ DOM.useEntropy = $(".use-entropy"); DOM.entropyContainer = $(".entropy-container"); DOM.entropy = $(".entropy"); - DOM.entropyError = $(".entropy-error"); + DOM.entropyFeedback = $(".entropy-feedback"); + DOM.entropyFiltered = DOM.entropyFeedback.find(".filtered"); + DOM.entropyType = DOM.entropyFeedback.find(".type"); + DOM.entropyStrength = DOM.entropyFeedback.find(".strength"); + DOM.entropyEventCount = DOM.entropyFeedback.find(".event-count"); + DOM.entropyBits = DOM.entropyFeedback.find(".bits"); + DOM.entropyBitsPerEvent = DOM.entropyFeedback.find(".bits-per-event"); + DOM.entropyMnemonicLength = DOM.entropyFeedback.find(".mnemonic-length"); DOM.phrase = $(".phrase"); DOM.passphrase = $(".passphrase"); DOM.generateContainer = $(".generate-container"); @@ -61,6 +68,7 @@ DOM.network.on("change", networkChanged); DOM.useEntropy.on("change", setEntropyVisibility); DOM.entropy.on("input", delayedEntropyChanged); + DOM.entropyMnemonicLength.on("change", entropyChanged); DOM.phrase.on("input", delayedPhraseChanged); DOM.passphrase.on("input", delayedPhraseChanged); DOM.generate.on("click", generateClicked); @@ -153,7 +161,7 @@ // If blank entropy, clear mnemonic, addresses, errors if (DOM.entropy.val().trim().length == 0) { clearDisplay(); - hideEntropyError(); + hideEntropyFeedback(); DOM.phrase.val(""); showValidationError("Blank entropy"); return; @@ -727,7 +735,7 @@ } function setMnemonicFromEntropy() { - hideEntropyError(); + hideEntropyFeedback(); // Get entropy value var entropyStr = DOM.entropy.val(); // Work out minimum base for entropy @@ -736,33 +744,26 @@ return; } // Show entropy details - var extraBits = 32 - (entropy.binaryStr.length % 32); - var extraChars = Math.ceil(extraBits * Math.log(2) / Math.log(entropy.base.asInt)); - var words = Math.floor(entropy.binaryStr.length / 32) * 3; - var strength = "an extremely weak"; - if (words >= 3) { - strength = "a very weak"; - } - if (words >= 6) { - strength = "a weak"; - } - if (words >= 9) { - strength = "a strong"; - } - if (words >= 12) { - strength = "a very strong"; - } - if (words >= 15) { - strength = "an extremely strong"; - } - if (words >= 18) { - strength = "an even stronger" + showEntropyFeedback(entropy); + // Use entropy hash if not using raw entropy + var bits = entropy.binaryStr; + var mnemonicLength = DOM.entropyMnemonicLength.val(); + if (mnemonicLength != "raw") { + // Get bits by hashing entropy with SHA256 + 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++) { + bits = "0" + bits; + } + // Truncate hash to suit number of words + mnemonicLength = parseInt(mnemonicLength); + var numberOfBits = 32 * mnemonicLength / 3; + bits = bits.substring(0, numberOfBits); } - var msg = "Have " + entropy.binaryStr.length + " bits of entropy, " + extraChars + " more " + entropy.base.str + " chars required to generate " + strength + " mnemonic: " + entropy.cleanStr; - showEntropyError(msg); // Discard trailing entropy - var bitsToUse = Math.floor(entropy.binaryStr.length / 32) * 32; - var binaryStr = entropy.binaryStr.substring(0, bitsToUse); + var bitsToUse = Math.floor(bits.length / 32) * 32; + var binaryStr = bits.substring(0, bitsToUse); // Convert entropy string to numeric array var entropyArr = []; for (var i=0; i= 64) { + strength = "very weak"; + } + if (entropy.binaryStr.length >= 96) { + strength = "weak"; + } + if (entropy.binaryStr.length >= 128) { + strength = "strong"; + } + if (entropy.binaryStr.length >= 160) { + strength = "very strong"; + } + if (entropy.binaryStr.length >= 192) { + strength = "extremely strong"; + } + // If time to crack is less than one day, and password is considered + // strong or better based on the number of bits, rename strength to + // 'easily cracked'. + var z = zxcvbn(entropy.cleanStr); + var timeToCrack = z.crack_times_seconds.offline_fast_hashing_1e10_per_second; + if (timeToCrack < 86400 && entropy.binaryStr.length >= 128) { + strength = "easily cracked"; + if (z.feedback.warning != "") { + strength = strength + " - " + z.feedback.warning; + }; + } + var bitsStr = entropy.binaryStr.length; + if (entropy.base.asInt != 2) { + bitsStr += " (" + entropy.binaryStr + ")"; + } + DOM.entropyFiltered.text(entropy.cleanStr); + DOM.entropyType.text(entropy.base.str); + DOM.entropyStrength.text(strength); + DOM.entropyEventCount.text(entropy.base.ints.length); + DOM.entropyBits.text(bitsStr); + DOM.entropyBitsPerEvent.text(Math.log2(entropy.base.asInt).toFixed(2)); + DOM.entropyFeedback.removeClass("hidden"); } var networks = [