Skip to content

Instantly share code, notes, and snippets.

@rndme
Created July 26, 2016 07:36
Show Gist options
  • Save rndme/6efad7b9a75968dbd0b9ec50d30362ea to your computer and use it in GitHub Desktop.
Save rndme/6efad7b9a75968dbd0b9ec50d30362ea 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"
function rc4stream(chars, iv) {
var s = [], key=iv || (Date.now() / (performance.now()*1000)).toString(36) + (chars/Math.random()),
j = 0,
x, res = [],
i, j, y, l = key.length,
m = Math.ceil(chars/2);
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++) {
j = (j + s[i = (i + 1) % 256]) % 256;
x = s[i];
s[i] = s[j];
s[j] = x;
res.push( (0+(80 ^ s[(s[i] + s[j]) % 256]).toString(16)).slice(-2) );
}
return res.join("").slice(0, chars);
}
//ex:
// rc4stream(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