]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/BIP39.git/blobdiff - src/js/entropy.js
Card suits have color and use larger font size
[perso/Immae/Projets/Cryptomonnaies/BIP39.git] / src / js / entropy.js
index cf981ff680ceae66c9a51f2dc23c84f1ef26d597..0b76dcfef0afbdfb8f8de5103bb090828718fa9f 100644 (file)
@@ -93,43 +93,10 @@ window.Entropy = new (function() {
             return {
                 binaryStr: "",
                 cleanStr: "",
+                cleanHtml: "",
                 base: base,
             };
         }
-        // Pull leading zeros off
-        var leadingZeros = [];
-        while (base.ints[0] == "0") {
-            leadingZeros.push("0");
-            base.ints.shift();
-        }
-        // Convert leading zeros to binary equivalent
-        var numBinLeadingZeros = Math.floor(Math.log2(base.asInt) * leadingZeros.length);
-        var binLeadingZeros = "";
-        for (var i=0; i<numBinLeadingZeros; i++) {
-            binLeadingZeros += "0";
-        }
-        // Handle entropy of zero
-        if (base.ints.length == 0) {
-            return {
-                binaryStr: binLeadingZeros,
-                cleanStr: leadingZeros.join(""),
-                base: base,
-            }
-        }
-        // If the first integer is small, it must be padded with zeros.
-        // Otherwise the chance of the first bit being 1 is 100%, which is
-        // obviously incorrect.
-        // This is not perfect for unusual bases, so is only done for bases
-        // of 2^n, eg octal or hexadecimal
-        if (base.asInt == 16) {
-            var firstInt = base.ints[0];
-            var firstIntBits = firstInt.toString(2).length;
-            var maxFirstIntBits = (base.asInt-1).toString(2).length;
-            var missingFirstIntBits = maxFirstIntBits - firstIntBits;
-            for (var i=0; i<missingFirstIntBits; i++) {
-                binLeadingZeros += "0";
-            }
-        }
         // Convert base.ints to BigInteger.
         // Due to using unusual bases, eg cards of base52, this is not as simple as
         // using BigInteger.parse()
@@ -140,12 +107,35 @@ window.Entropy = new (function() {
             var additionalEntropy = BigInteger.parse(base.asInt).pow(power).multiply(thisInt);
             entropyInt = entropyInt.add(additionalEntropy);
         }
-        // Convert entropy to different formats
-        var entropyBin = binLeadingZeros + entropyInt.toString(2);
+        // Convert entropy to binary
+        var entropyBin = entropyInt.toString(2);
+        // If the first integer is small, it must be padded with zeros.
+        // Otherwise the chance of the first bit being 1 is 100%, which is
+        // obviously incorrect.
+        // This is not perfect for non-2^n bases.
+        var expectedBits = Math.floor(base.parts.length * Math.log2(base.asInt));
+        while (entropyBin.length < expectedBits) {
+            entropyBin = "0" + entropyBin;
+        }
+        // Supply a 'filtered' entropy string for display purposes
         var entropyClean = base.parts.join("");
+        var entropyHtml = base.parts.join("");
+        if (base.asInt == 52) {
+            entropyClean = base.parts.join(" ").toUpperCase();
+            entropyClean = entropyClean.replace(/C/g, "\u2663");
+            entropyClean = entropyClean.replace(/D/g, "\u2666");
+            entropyClean = entropyClean.replace(/H/g, "\u2665");
+            entropyClean = entropyClean.replace(/S/g, "\u2660");
+            entropyHtml = base.parts.join(" ").toUpperCase();
+            entropyHtml = entropyHtml.replace(/C/g, "<span class='card-suit club'>\u2663</span>");
+            entropyHtml = entropyHtml.replace(/D/g, "<span class='card-suit diamond'>\u2666</span>");
+            entropyHtml = entropyHtml.replace(/H/g, "<span class='card-suit heart'>\u2665</span>");
+            entropyHtml = entropyHtml.replace(/S/g, "<span class='card-suit spade'>\u2660</span>");
+        }
         var e = {
             binaryStr: entropyBin,
             cleanStr: entropyClean,
+            cleanHtml: entropyHtml,
             base: base,
         }
         return e;