+ function isUsingOwnEntropy() {
+ return DOM.useEntropy.prop("checked");
+ }
+
+ function setMnemonicFromEntropy() {
+ clearEntropyFeedback();
+ // Get entropy value
+ var entropyStr = DOM.entropy.val();
+ // Work out minimum base for entropy
+ var entropy = Entropy.fromString(entropyStr);
+ if (entropy.binaryStr.length == 0) {
+ return;
+ }
+ // Show entropy details
+ 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);
+ }
+ // Discard trailing entropy
+ 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<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
+ var phrase = mnemonic.toMnemonic(entropyArr);
+ // Set the mnemonic in the UI
+ DOM.phrase.val(phrase);
+ }
+
+ 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'.
+ 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;
+ var wordCount = Math.floor(entropy.binaryStr.length / 32) * 3;
+ DOM.entropyFiltered.html(entropy.cleanHtml);
+ DOM.entropyType.text(entropy.base.str);
+ 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));
+ }
+