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.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;
}
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 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<binaryStr.length / 8; i++) {
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 showEntropyFeedback(entropy) {
+ var strength = "extremely weak";
+ if (entropy.binaryStr.length >= 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 totalDecks = Math.ceil(entropy.base.parts.length / 52);
+ var totalCards = totalDecks * 52;
+ var totalCombos = factorial(52).pow(totalDecks);
+ var totalRemainingCards = totalCards - entropy.base.parts.length;
+ var remainingDecks = Math.floor(totalRemainingCards / 52);
+ var remainingCards = totalRemainingCards % 52;
+ var remainingCombos = factorial(52).pow(remainingDecks) * 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<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");
+ }
+ // 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 = [