X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2Fjs%2Findex.js;h=dbbd382e406de4ca6e999e3ad62d880e335e8089;hb=9bc39377aa0317ffbfed3b6e29814bb26681cfd5;hp=a717a9e45bdd0369004ba521a0a25b008ee67ae9;hpb=adc8ce127d4f8ea0d7e5ede6a82c2791d60ff4d2;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FBIP39.git diff --git a/src/js/index.js b/src/js/index.js index a717a9e..dbbd382 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -24,7 +24,15 @@ DOM.useEntropy = $(".use-entropy"); DOM.entropyContainer = $(".entropy-container"); DOM.entropy = $(".entropy"); - DOM.entropyError = $(".entropy-error"); + DOM.entropyFiltered = DOM.entropyContainer.find(".filtered"); + DOM.entropyType = DOM.entropyContainer.find(".type"); + DOM.entropyStrength = DOM.entropyContainer.find(".strength"); + DOM.entropyEventCount = DOM.entropyContainer.find(".event-count"); + DOM.entropyBits = DOM.entropyContainer.find(".bits"); + DOM.entropyBitsPerEvent = DOM.entropyContainer.find(".bits-per-event"); + DOM.entropyWordCount = DOM.entropyContainer.find(".word-count"); + DOM.entropyBinary = DOM.entropyContainer.find(".binary"); + DOM.entropyMnemonicLength = DOM.entropyContainer.find(".mnemonic-length"); DOM.phrase = $(".phrase"); DOM.passphrase = $(".passphrase"); DOM.generateContainer = $(".generate-container"); @@ -43,7 +51,7 @@ DOM.bip44coin = $("#bip44 .coin"); DOM.bip44account = $("#bip44 .account"); DOM.bip44change = $("#bip44 .change"); - DOM.strength = $(".strength"); + DOM.generatedStrength = $(".generate-container .strength"); DOM.hardenedAddresses = $(".hardened-addresses"); DOM.addresses = $(".addresses"); DOM.rowsToAdd = $(".rows-to-add"); @@ -61,6 +69,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 +162,7 @@ // If blank entropy, clear mnemonic, addresses, errors if (DOM.entropy.val().trim().length == 0) { clearDisplay(); - hideEntropyError(); + clearEntropyFeedback(); DOM.phrase.val(""); showValidationError("Blank entropy"); return; @@ -286,7 +295,7 @@ showValidationError(errorText); return; } - var numWords = parseInt(DOM.strength.val()); + var numWords = parseInt(DOM.generatedStrength.val()); var strength = numWords / 3 * 32; var words = mnemonic.generate(strength); DOM.phrase.val(words); @@ -727,7 +736,7 @@ } function setMnemonicFromEntropy() { - hideEntropyError(); + clearEntropyFeedback(); // Get entropy value var entropyStr = DOM.entropy.val(); // Work out minimum base for entropy @@ -736,33 +745,27 @@ 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 start = bits.length - bitsToUse; + var binaryStr = bits.substring(start); // 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'. + try { + var z = zxcvbn(entropy.base.parts.join("")); + 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; + }; + } + } + catch (e) { + strength = "unknown"; + console.log("Error detecting entropy strength with zxcvbn:"); + console.log(e); + } + var bitsStr = getNumberOfEntropyBits(entropy); + var wordCount = Math.floor(entropy.binaryStr.length / 32) * 3; + var entropyTypeStr = getEntropyTypeStr(entropy); + DOM.entropyFiltered.html(entropy.cleanHtml); + DOM.entropyType.text(entropyTypeStr); + DOM.entropyStrength.text(strength); + DOM.entropyEventCount.text(entropy.base.ints.length); + DOM.entropyBits.text(bitsStr); + DOM.entropyWordCount.text(wordCount); + DOM.entropyBinary.text(entropy.binaryStr); + DOM.entropyBitsPerEvent.text(Math.log2(entropy.base.asInt).toFixed(2)); + } + + function getNumberOfEntropyBits(entropy) { + var bitsStr = entropy.binaryStr.length.toString(); + // If using cards, assume they are not reused, thus additional entropy + // decreases as more cards are used. This means entropy is measured + // using n!, not base^n. + // eg the second last card can be only one of two, not one of fifty two + // so the added entropy for that card is only one bit at most + if (entropy.base.asInt == 52) { + var totalCombos = factorial(52); + var remainingCards = 52 - entropy.base.parts.length; + var remainingCombos = factorial(remainingCards); + var currentCombos = totalCombos.divide(remainingCombos); + bitsStr = currentCombos.toString(2).length.toString(); + } + return bitsStr + } + + function getEntropyTypeStr(entropy) { + var typeStr = entropy.base.str; + // Add some detail if these are cards + if (entropy.base.asInt == 52) { + var cardDetail = []; // array of message strings + // Detect duplicates + var dupes = []; + var dupeTracker = {}; + for (var i=0; i 0) { + var dupeWord = "duplicates"; + if (dupes.length == 1) { + dupeWord = "duplicate"; + } + var msg = dupes.length + " " + dupeWord + ": " + dupes.slice(0,3).join(" "); + if (dupes.length > 3) { + msg += "..."; + } + cardDetail.push(msg); + } + // Detect full deck + var uniqueCards = []; + for (var uniqueCard in dupeTracker) { + uniqueCards.push(uniqueCard); + } + if (uniqueCards.length == 52) { + cardDetail.unshift("full deck"); + } + // Add card details to typeStr + if (cardDetail.length > 0) { + typeStr += " (" + cardDetail.join(", ") + ")"; + } + } + return typeStr; } - function showEntropyError(msg) { - DOM.entropyError.text(msg); - DOM.entropyError.removeClass("hidden"); + // Depends on BigInteger + function factorial(n) { + if (n == 0) { + return 1; + } + f = BigInteger.ONE; + for (var i=1; i<=n; i++) { + f = f.multiply(new BigInteger(i)); + } + return f; } var networks = [