Created
May 11, 2016 13:06
-
-
Save rndme/d0f897af63cc39f9270e708490abe239 to your computer and use it in GitHub Desktop.
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 spritz(key, str) { | |
var s = [], | |
j = 0, | |
w = 17, | |
k = 0, | |
res = [], | |
l = key.length, | |
m = str.length, | |
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(z = str.charCodeAt(y) ^ s[j + s[i + s[z + k]]]); | |
} | |
return String.fromCharCode.apply(0, res); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment