Last active
July 26, 2016 15:24
-
-
Save rndme/9609d300d8b3ed5d8329d4825ffe5824 to your computer and use it in GitHub Desktop.
given a seed value "iv" (optional), returns a deterministic string of pseudo-random hex values of length "chars"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// public domain, based on https://www.schneier.com/blog/archives/2014/10/spritz_a_new_rc.html | |
function spritzStream(chars, iv) { | |
var s = [], key=iv || (Date.now() / (performance.now()*1000)).toString(36) + (chars/Math.random()), | |
j = 0, | |
w = 17, | |
k = 0, | |
res = [], | |
l = Math.ceil(key.length/2), | |
m = chars, | |
z, x, i, y; | |
for(i = 0; i < 256; i++) s[i] = i; | |
for(i = 0; i < 256; i++) { | |
j = (j + s[i] + key.charCodeAt(i % l)) % 256; | |
x = s[i]; | |
s[i] = s[j]; | |
s[j] = x; | |
} | |
for(y = 0, i = 0, j = 0; y < m; y++) { | |
i = (i + w) % 256; | |
j = (k + s[j + s[i]]) % 256; | |
k = (i + k + s[j]) % 256; | |
x = s[i]; | |
s[i] = s[j]; | |
s[j] = x; //swap step | |
res.push( (0+(z = (y%255) ^ s[j + s[i + s[z + k]]]).toString(16)).slice(-2)); | |
} | |
return res.join("").slice(0, chars); | |
} | |
//ex: | |
//spritzStream(500, "this is not a great iv"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment