From b6709863c443d0093512b83d4592871bcfdea45f Mon Sep 17 00:00:00 2001 From: Patrick Hurley Date: Mon, 16 Mar 2020 22:26:39 -0400 Subject: [PATCH] Support keys --- decrypto.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ index.html | 1 + 2 files changed, 54 insertions(+) diff --git a/decrypto.js b/decrypto.js index 92ce948..b4606e4 100644 --- a/decrypto.js +++ b/decrypto.js @@ -14,6 +14,43 @@ function loadWordList() { return wordList; } +function xmur3(str) { + for(var i = 0, h = 1779033703 ^ str.length; i < str.length; i++) + h = Math.imul(h ^ str.charCodeAt(i), 3432918353), + h = h << 13 | h >>> 19; + return function() { + h = Math.imul(h ^ h >>> 16, 2246822507); + h = Math.imul(h ^ h >>> 13, 3266489909); + return (h ^= h >>> 16) >>> 0; + } +} + +function sfc32(a, b, c, d) { + return function() { + a >>>= 0; b >>>= 0; c >>>= 0; d >>>= 0; + var t = (a + b) | 0; + a = b ^ b >>> 9; + b = c + (c << 3) | 0; + c = (c << 21 | c >>> 11); + d = d + 1 | 0; + t = t + d | 0; + c = c + t | 0; + return (t >>> 0) / 4294967296; + } +} + +var my_random = function() { + return Math.random; +} + +_.random = function(min, max) { + if (max == null) { + max = min; + min = 0; + } + return min + Math.floor(my_random() * (max - min + 1)); +}; + function pickWords(numWords) { return _.sample(wordList, numWords); } @@ -51,10 +88,23 @@ function loadCode() { } } +var seed = function() { return 0; }; + function newGame() { + var seedkey = document.getElementById("gameseed").value; + if (seedkey.length > 0) { + seed = xmur3(seedkey); + } else { + seed = xmur3((new Date()).toJSON()); + } + my_random = sfc32(seed(), seed(), seed(), seed()); var words = pickWords(4); Cookies.set("words", words); Cookies.remove("code"); + + seed = xmur3((new Date()).toJSON()); + my_random = sfc32(seed(), seed(), seed(), seed()); + $('#revealCodeButton').hide(); return words; } @@ -111,6 +161,9 @@ function initialize() { wordList = loadWordList(); loadCode(); + seed = xmur3((new Date()).toJSON()); + my_random = sfc32(seed(), seed(), seed(), seed()); + var words = loadWords(); if (words) { diff --git a/index.html b/index.html index c77af66..58fc4ec 100644 --- a/index.html +++ b/index.html @@ -144,6 +144,7 @@