diff options
author | Ian Coleman <coleman.ian@gmail.com> | 2016-11-16 11:46:25 +1100 |
---|---|---|
committer | Ian Coleman <coleman.ian@gmail.com> | 2016-11-16 11:58:26 +1100 |
commit | 391c7f267f6be3356d0bffa74e2726245b5f45b2 (patch) | |
tree | 0db89de6cbbf6ab1c45a6ce53b6301d9724245d8 /src | |
parent | 02f05d3e467ba8ccf3fca2811eda847fc71e511f (diff) | |
download | BIP39-391c7f267f6be3356d0bffa74e2726245b5f45b2.tar.gz BIP39-391c7f267f6be3356d0bffa74e2726245b5f45b2.tar.zst BIP39-391c7f267f6be3356d0bffa74e2726245b5f45b2.zip |
Card duplicates and use of full deck is detected
Diffstat (limited to 'src')
-rw-r--r-- | src/js/index.js | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/src/js/index.js b/src/js/index.js index 89f8d0d..e064185 100644 --- a/src/js/index.js +++ b/src/js/index.js | |||
@@ -820,8 +820,9 @@ | |||
820 | } | 820 | } |
821 | var bitsStr = getNumberOfEntropyBits(entropy); | 821 | var bitsStr = getNumberOfEntropyBits(entropy); |
822 | var wordCount = Math.floor(entropy.binaryStr.length / 32) * 3; | 822 | var wordCount = Math.floor(entropy.binaryStr.length / 32) * 3; |
823 | var entropyTypeStr = getEntropyTypeStr(entropy); | ||
823 | DOM.entropyFiltered.html(entropy.cleanHtml); | 824 | DOM.entropyFiltered.html(entropy.cleanHtml); |
824 | DOM.entropyType.text(entropy.base.str); | 825 | DOM.entropyType.text(entropyTypeStr); |
825 | DOM.entropyStrength.text(strength); | 826 | DOM.entropyStrength.text(strength); |
826 | DOM.entropyEventCount.text(entropy.base.ints.length); | 827 | DOM.entropyEventCount.text(entropy.base.ints.length); |
827 | DOM.entropyBits.text(bitsStr); | 828 | DOM.entropyBits.text(bitsStr); |
@@ -847,6 +848,48 @@ | |||
847 | return bitsStr | 848 | return bitsStr |
848 | } | 849 | } |
849 | 850 | ||
851 | function getEntropyTypeStr(entropy) { | ||
852 | var typeStr = entropy.base.str; | ||
853 | // Add some detail if these are cards | ||
854 | if (entropy.base.asInt == 52) { | ||
855 | var cardDetail = []; // array of message strings | ||
856 | // Detect duplicates | ||
857 | var dupes = []; | ||
858 | var dupeTracker = {}; | ||
859 | for (var i=0; i<entropy.base.parts.length; i++) { | ||
860 | var card = entropy.base.parts[i]; | ||
861 | if (card in dupeTracker) { | ||
862 | dupes.push(card); | ||
863 | } | ||
864 | dupeTracker[card] = true; | ||
865 | } | ||
866 | if (dupes.length > 0) { | ||
867 | var dupeWord = "duplicates"; | ||
868 | if (dupes.length == 1) { | ||
869 | dupeWord = "duplicate"; | ||
870 | } | ||
871 | var msg = dupes.length + " " + dupeWord + ": " + dupes.slice(0,3).join(" "); | ||
872 | if (dupes.length > 3) { | ||
873 | msg += "..."; | ||
874 | } | ||
875 | cardDetail.push(msg); | ||
876 | } | ||
877 | // Detect full deck | ||
878 | var uniqueCards = []; | ||
879 | for (var uniqueCard in dupeTracker) { | ||
880 | uniqueCards.push(uniqueCard); | ||
881 | } | ||
882 | if (uniqueCards.length == 52) { | ||
883 | cardDetail.unshift("full deck"); | ||
884 | } | ||
885 | // Add card details to typeStr | ||
886 | if (cardDetail.length > 0) { | ||
887 | typeStr += " (" + cardDetail.join(", ") + ")"; | ||
888 | } | ||
889 | } | ||
890 | return typeStr; | ||
891 | } | ||
892 | |||
850 | // Depends on BigInteger | 893 | // Depends on BigInteger |
851 | function factorial(n) { | 894 | function factorial(n) { |
852 | if (n == 0) { | 895 | if (n == 0) { |