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");
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");
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);
// If blank entropy, clear mnemonic, addresses, errors
if (DOM.entropy.val().trim().length == 0) {
clearDisplay();
- hideEntropyError();
+ clearEntropyFeedback();
DOM.phrase.val("");
showValidationError("Blank entropy");
return;
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);
}
function setMnemonicFromEntropy() {
- hideEntropyError();
+ clearEntropyFeedback();
// Get entropy value
var entropyStr = DOM.entropy.val();
// Work out minimum base for entropy
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 strength = "an extremely weak";
- if (entropy.hexStr.length >= 8) {
- strength = "a very weak";
- }
- if (entropy.hexStr.length >= 12) {
- strength = "a weak";
- }
- if (entropy.hexStr.length >= 24) {
- strength = "a strong";
- }
- if (entropy.hexStr.length >= 32) {
- strength = "a very strong";
- }
- if (entropy.hexStr.length >= 40) {
- strength = "an extremely strong";
- }
- if (entropy.hexStr.length >=48) {
- 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 hexStr = entropy.hexStr.substring(0, Math.floor(entropy.hexStr.length / 8) * 8);
+ 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<hexStr.length / 2; i++) {
- var entropyByte = parseInt(hexStr[i*2].concat(hexStr[i*2+1]), 16);
+ for (var i=0; i<binaryStr.length / 8; i++) {
+ var byteAsBits = binaryStr.substring(i*8, i*8+8);
+ var entropyByte = parseInt(byteAsBits, 2);
entropyArr.push(entropyByte)
}
// Convert entropy array to mnemonic
DOM.phrase.val(phrase);
}
- function hideEntropyError() {
- DOM.entropyError.addClass("hidden");
+ function clearEntropyFeedback() {
+ DOM.entropyStrength.text("...");
+ DOM.entropyType.text("");
+ DOM.entropyWordCount.text("0");
+ DOM.entropyEventCount.text("0");
+ DOM.entropyBitsPerEvent.text("0");
+ DOM.entropyBits.text("0");
+ DOM.entropyFiltered.html(" ");
+ DOM.entropyBinary.html(" ");
}
- function showEntropyError(msg) {
- DOM.entropyError.text(msg);
- DOM.entropyError.removeClass("hidden");
+ function showEntropyFeedback(entropy) {
+ var numberOfBits = entropy.binaryStr.length;
+ var strength = "extremely weak";
+ if (numberOfBits >= 64) {
+ strength = "very weak";
+ }
+ if (numberOfBits >= 96) {
+ strength = "weak";
+ }
+ if (numberOfBits >= 128) {
+ strength = "strong";
+ }
+ if (numberOfBits >= 160) {
+ strength = "very strong";
+ }
+ if (numberOfBits >= 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 entropyTypeStr = getEntropyTypeStr(entropy);
+ var wordCount = Math.floor(numberOfBits / 32) * 3;
+ var bitsPerEvent = Math.log2(entropy.base.asInt).toFixed(2);
+ if (entropy.base.asInt == 52) {
+ bitsPerEvent = bitsPerEvent + " (or less)";
+ }
+ DOM.entropyFiltered.html(entropy.cleanHtml);
+ DOM.entropyType.text(entropyTypeStr);
+ DOM.entropyStrength.text(strength);
+ DOM.entropyEventCount.text(entropy.base.ints.length);
+ DOM.entropyBits.text(numberOfBits);
+ DOM.entropyWordCount.text(wordCount);
+ DOM.entropyBinary.text(entropy.binaryStr);
+ DOM.entropyBitsPerEvent.text(bitsPerEvent);
+ }
+
+ 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<entropy.base.parts.length; i++) {
+ var card = entropy.base.parts[i];
+ var cardUpper = card.toUpperCase();
+ if (cardUpper in dupeTracker) {
+ dupes.push(card);
+ }
+ dupeTracker[cardUpper] = true;
+ }
+ if (dupes.length > 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");
+ }
+ // Detect missing cards
+ var values = "A23456789TJQK";
+ var suits = "CDHS";
+ var missingCards = [];
+ for (var i=0; i<suits.length; i++) {
+ for (var j=0; j<values.length; j++) {
+ var card = values[j] + suits[i];
+ if (!(card in dupeTracker)) {
+ missingCards.push(card);
+ }
+ }
+ }
+ // Display missing cards if six or less, ie clearly going for full deck
+ if (missingCards.length > 0 && missingCards.length <= 6) {
+ var msg = missingCards.length + " missing: " + missingCards.slice(0,3).join(" ");
+ if (missingCards.length > 3) {
+ msg += "...";
+ }
+ cardDetail.push(msg);
+ }
+ // Add card details to typeStr
+ if (cardDetail.length > 0) {
+ typeStr += " (" + cardDetail.join(", ") + ")";
+ }
+ }
+ return typeStr;
}
var networks = [