Skip to content

Instantly share code, notes, and snippets.

@rndme
Created May 11, 2016 13:06
Show Gist options
  • Save rndme/d0f897af63cc39f9270e708490abe239 to your computer and use it in GitHub Desktop.
Save rndme/d0f897af63cc39f9270e708490abe239 to your computer and use it in GitHub Desktop.
//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