Comparing version 1.2.0 to 2.0.0
@@ -0,1 +1,13 @@ | ||
v2.0.0 / 2014-07-10 | ||
================== | ||
* Rename "codeByte" to "coreArray" | ||
* New structure of code | ||
v1.2.0 / 2014-07-08 | ||
================== | ||
* `code()` fix | ||
* Buffer methods | ||
v1.1.1 / 2014-06-29 | ||
@@ -2,0 +14,0 @@ ================== |
441
index.js
@@ -7,3 +7,3 @@ "use strict"; | ||
* @subpackage main | ||
* @version 1.2.0 | ||
* @version 2.0.0 | ||
* @author hex7c0 <hex7c0@gmail.com> | ||
@@ -18,8 +18,8 @@ * @copyright hex7c0 2014 | ||
/** | ||
* build sbox | ||
* generate sbox | ||
* | ||
* @function sbox | ||
* @function gSbox | ||
* @return {Array} | ||
*/ | ||
function sbox() { | ||
function gSbox() { | ||
@@ -43,18 +43,170 @@ return [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24, | ||
/** | ||
* convert string to byte | ||
* generate ksa | ||
* | ||
* @function bytearray | ||
* @param {String} str - user key | ||
* @function gKsa | ||
* @param {Array} key - user key | ||
* @return {Array} | ||
*/ | ||
function bytearray(str) { | ||
function gKsa(key) { | ||
var l = str.length; | ||
var bytes = new Array(l); | ||
for (var i = 0; i < l; i++) { | ||
bytes[i] = (str.charCodeAt(i)); | ||
var j = 0; | ||
var s = gSbox(); | ||
var ke = key; | ||
var len = ke.length; | ||
for (var i = 0; i < 256; i++) { | ||
j = (j + s[i] + ke[i % len]) % 256; | ||
s[j] = [s[i],s[i] = s[j]][0]; | ||
} | ||
return bytes; | ||
return s; | ||
}; | ||
/** | ||
* body cipher | ||
* | ||
* @function body | ||
* @param {String|Array|Buffer} inp - input | ||
* @param {String|Array|Buffer} res - response | ||
* @param {Array} ksa - ksa box | ||
* @return {String|Array|Buffer} | ||
*/ | ||
function body(inp,res,ksa) { | ||
var i = 0, j = 0; | ||
var s = ksa; | ||
var res = res; | ||
if (typeof (res) == 'string') { | ||
for (var y = 0, l = inp.length; y < l; y++) { | ||
i = (i + 1) % 256; | ||
j = (j + s[i]) % 256; | ||
s[j] = [s[i],s[i] = s[j]][0]; | ||
res += String.fromCharCode(inp.charCodeAt(y) | ||
^ s[(s[i] + s[j]) % 256]); | ||
} | ||
} else { | ||
for (var y = 0, l = inp.length; y < l; y++) { | ||
i = (i + 1) % 256; | ||
j = (j + s[i]) % 256; | ||
s[j] = [s[i],s[i] = s[j]][0]; | ||
res[y] = inp[y] ^ s[(s[i] + s[j]) % 256]; | ||
} | ||
} | ||
return res; | ||
} | ||
/** | ||
* body cipher RC4A | ||
* | ||
* @function bodyRC4A | ||
* @param {String|Array|Buffer} inp - input | ||
* @param {String|Array|Buffer} res - response | ||
* @param {Array} ksa - ksa box | ||
* @return {String|Array|Buffer} | ||
*/ | ||
function bodyRC4A(inp,res,ksa) { | ||
var i = 0, j1 = 0, j2 = 0; | ||
var s1 = ksa; | ||
var s2 = s1.slice(); | ||
var res = res; | ||
if (typeof (res) == 'string') { | ||
for (var y = 0, l = inp.length; y < l; y++) { | ||
i = (i + 1) % 256; | ||
j1 = (j1 + s1[i]) % 256; | ||
s1[j1] = [s1[i],s1[i] = s1[j1]][0]; | ||
res += String.fromCharCode(inp.charCodeAt(y) | ||
^ s2[(s1[i] + s1[j1]) % 256]); | ||
y++; | ||
j2 = (j2 + s2[i]) % 256; | ||
s2[j2] = [s2[i],s2[i] = s2[j2]][0]; | ||
res += String.fromCharCode(inp.charCodeAt(y) | ||
^ s1[(s2[i] + s2[j2]) % 256]); | ||
} | ||
} else { | ||
for (var y = 0, l = inp.length; y < l; y++) { | ||
i = (i + 1) % 256; | ||
j1 = (j1 + s1[i]) % 256; | ||
s1[j1] = [s1[i],s1[i] = s1[j1]][0]; | ||
res[y] = inp[y] ^ s2[(s1[i] + s1[j1]) % 256]; | ||
y++; | ||
j2 = (j2 + s2[i]) % 256; | ||
s2[j2] = [s2[i],s2[i] = s2[j2]][0]; | ||
res[y] = inp[y] ^ s1[(s2[i] + s2[j1]) % 256]; | ||
} | ||
} | ||
return res; | ||
}; | ||
/** | ||
* body cipher VMPC | ||
* | ||
* @function bodyVMPC | ||
* @param {String|Array|Buffer} inp - input | ||
* @param {String|Array|Buffer} res - response | ||
* @param {Array} ksa - ksa box | ||
* @return {String|Array|Buffer} | ||
*/ | ||
function bodyVMPC(inp,res,ksa) { | ||
var i = 0, j = 0; | ||
var s = ksa; | ||
var a = null, b = null; | ||
var res = res; | ||
if (typeof (res) == 'string') { | ||
for (var y = 0, l = inp.length; y < l; y++) { | ||
a = s[i]; | ||
j = s[(j + a) % 256]; | ||
b = s[j]; | ||
res += String.fromCharCode(inp.charCodeAt(y) ^ s[s[b] + 1]); | ||
s[j] = [a,s[i] = b][0]; | ||
i = (i + 1) % 256; | ||
} | ||
} else { | ||
for (var y = 0, l = inp.length; y < l; y++) { | ||
a = s[i]; | ||
j = s[(j + a) % 256]; | ||
b = s[j]; | ||
res[y] = inp[y] ^ s[s[b] + 1]; | ||
s[j] = [a,s[i] = b][0]; | ||
i = (i + 1) % 256; | ||
} | ||
} | ||
return res; | ||
}; | ||
/** | ||
* body cipher RC4p | ||
* | ||
* @function bodyRC4p | ||
* @param {String|Array|Buffer} inp - input | ||
* @param {String|Array|Buffer} res - response | ||
* @param {Array} ksa - ksa box | ||
* @return {String|Array|Buffer} | ||
*/ | ||
function bodyRC4p(inp,res,ksa) { | ||
var i = 0, j = 0; | ||
var s = ksa; | ||
var a = null, b = null, c = null; | ||
var res = res; | ||
if (typeof (res) == 'string') { | ||
for (var y = 0, l = inp.length; y < l; y++) { | ||
i = (i + 1) % 256; | ||
a = s[i]; | ||
j = s[(j + a) % 256]; | ||
b = s[j]; | ||
s[j] = [a,s[i] = b][0]; | ||
c = (s[i << 5 ^ j >> 3] + s[j << 5 ^ i >> 3]) % 256; | ||
res += String.fromCharCode(inp.charCodeAt(y) | ||
^ (s[a + b] + s[c ^ 0xAA]) ^ s[j + b]); | ||
} | ||
} else { | ||
for (var y = 0, l = inp.length; y < l; y++) { | ||
i = (i + 1) % 256; | ||
a = s[i]; | ||
j = s[(j + a) % 256]; | ||
b = s[j]; | ||
s[j] = [a,s[i] = b][0]; | ||
c = (s[i << 5 ^ j >> 3] + s[j << 5 ^ i >> 3]) % 256; | ||
res[y] = inp[y] ^ (s[a + b] + s[c ^ 0xAA]) ^ s[j + b]; | ||
} | ||
} | ||
return res; | ||
}; | ||
/** | ||
* export class | ||
@@ -83,6 +235,4 @@ * | ||
this.key = ''; | ||
this.key; | ||
this.change(key); | ||
this.len = this.key.length; | ||
return; | ||
} | ||
@@ -93,3 +243,3 @@ /** | ||
* @function change | ||
* @param {String!Array} key - user key | ||
* @param {String|Array|Buffer} key - user key | ||
* @return | ||
@@ -99,8 +249,12 @@ */ | ||
if (typeof (key) == 'string') { | ||
this.key = bytearray(key); | ||
} else if (Array.isArray(key)) { | ||
this.key = new Array(key.legth); | ||
if (Array.isArray(key)) { | ||
this.key = key; | ||
} else if (typeof (key) == 'string' || Buffer.isBuffer(key)) { | ||
key = new Buffer(key); | ||
for (var i = 0, ii = key.length; i < ii; i++) { | ||
this.key[i] = key[i]; | ||
} | ||
} else { | ||
throw new Error('Invalid key'); | ||
throw new Error('Invalid data'); | ||
} | ||
@@ -110,20 +264,2 @@ return; | ||
/** | ||
* generate ksa | ||
* | ||
* @function ksa | ||
* @return {Array} | ||
*/ | ||
RC4.prototype.ksa = function(key) { | ||
var j = 0; | ||
var s = sbox(); | ||
var key = this.key; | ||
var len = this.len; | ||
for (var i = 0; i < 256; i++) { | ||
j = (j + s[i] + key[i % len]) % 256; | ||
s[j] = [s[i],s[i] = s[j]][0]; | ||
} | ||
return s; | ||
}; | ||
/** | ||
* RC4 string code | ||
@@ -137,32 +273,14 @@ * | ||
var res = ''; | ||
var i = 0, j = 0; | ||
var s = this.ksa(); | ||
for (var y = 0, l = str.length; y < l; y++) { | ||
i = (i + 1) % 256; | ||
j = (j + s[i]) % 256; | ||
s[j] = [s[i],s[i] = s[j]][0]; | ||
res += String.fromCharCode(str.charCodeAt(y) ^ s[(s[i] + s[j]) % 256]); | ||
} | ||
return res; | ||
return body(str,'',gKsa(this.key)); | ||
}; | ||
/** | ||
* RC4 byte code | ||
* RC4 array code | ||
* | ||
* @function codeByte | ||
* @param {Array} byt - data | ||
* @function codeArray | ||
* @param {Array} arr - data | ||
* @return {Array} | ||
*/ | ||
RC4.prototype.codeByte = function(byt) { | ||
RC4.prototype.codeArray = function(arr) { | ||
var res = new Array(byt.length); | ||
var i = 0, j = 0; | ||
var s = this.ksa(); | ||
for (var y = 0, l = byt.length; y < l; y++) { | ||
i = (i + 1) % 256; | ||
j = (j + s[i]) % 256; | ||
s[j] = [s[i],s[i] = s[j]][0]; | ||
res[y] = byt[y] ^ s[(s[i] + s[j]) % 256]; | ||
} | ||
return res; | ||
return body(arr,new Array(arr.length),gKsa(this.key)); | ||
}; | ||
@@ -178,12 +296,3 @@ /** | ||
var res = new Buffer(buff.length); | ||
var i = 0, j = 0; | ||
var s = this.ksa(); | ||
for (var y = 0, l = buff.length; y < l; y++) { | ||
i = (i + 1) % 256; | ||
j = (j + s[i]) % 256; | ||
s[j] = [s[i],s[i] = s[j]][0]; | ||
res[y] = buff[y] ^ s[(s[i] + s[j]) % 256]; | ||
} | ||
return res; | ||
return body(buff,new Buffer(buff.length),gKsa(this.key)); | ||
}; | ||
@@ -194,4 +303,4 @@ /** | ||
* @function code | ||
* @param {String|Array} boh - data | ||
* @return {String|Array} | ||
* @param {String|Array|Buffer} boh - data | ||
* @return {String|Array|Buffer} | ||
*/ | ||
@@ -203,3 +312,3 @@ RC4.prototype.code = function(boh) { | ||
} else if (Array.isArray(boh)) { | ||
return this.codeByte(boh); | ||
return this.codeArray(boh); | ||
} else if (Buffer.isBuffer(boh)) { | ||
@@ -221,44 +330,14 @@ return this.codeBuffer(boh); | ||
var res = ''; | ||
var i = 0, j1 = 0, j2 = 0; | ||
var s1 = this.ksa(); | ||
var s2 = s1.slice(); | ||
for (var y = 0, l = str.length; y < l; y++) { | ||
i = (i + 1) % 256; | ||
j1 = (j1 + s1[i]) % 256; | ||
s1[j1] = [s1[i],s1[i] = s1[j1]][0]; | ||
res += String.fromCharCode(str.charCodeAt(y) | ||
^ s2[(s1[i] + s1[j1]) % 256]); | ||
y++; | ||
j2 = (j2 + s2[i]) % 256; | ||
s2[j2] = [s2[i],s2[i] = s2[j2]][0]; | ||
res += String.fromCharCode(str.charCodeAt(y) | ||
^ s1[(s2[i] + s2[j2]) % 256]); | ||
} | ||
return res; | ||
return bodyRC4A(str,'',gKsa(this.key)); | ||
}; | ||
/** | ||
* RC4A byte code | ||
* RC4A array code | ||
* | ||
* @function codeByteRC4A | ||
* @param {Array} byt - data | ||
* @function codeArrayRC4A | ||
* @param {Array} arr - data | ||
* @return {Array} | ||
*/ | ||
RC4.prototype.codeByteRC4A = function(byt) { | ||
RC4.prototype.codeArrayRC4A = function(arr) { | ||
var res = new Array(byt.length); | ||
var i = 0, j1 = 0, j2 = 0; | ||
var s1 = this.ksa(); | ||
var s2 = s1.slice(); | ||
for (var y = 0, l = byt.length; y < l; y++) { | ||
i = (i + 1) % 256; | ||
j1 = (j1 + s1[i]) % 256; | ||
s1[j1] = [s1[i],s1[i] = s1[j1]][0]; | ||
res[y] = byt[y] ^ s2[(s1[i] + s1[j1]) % 256]; | ||
y++; | ||
j2 = (j2 + s2[i]) % 256; | ||
s2[j2] = [s2[i],s2[i] = s2[j2]][0]; | ||
res[y] = byt[y] ^ s1[(s2[i] + s2[j1]) % 256]; | ||
} | ||
return res; | ||
return bodyRC4A(arr,new Array(arr.length),gKsa(this.key)); | ||
}; | ||
@@ -268,23 +347,9 @@ /** | ||
* | ||
* @function codeByfferRC4A | ||
* @function codeBufferRC4A | ||
* @param {Buffer} buff - data | ||
* @return {Buffer} | ||
*/ | ||
RC4.prototype.codeByfferRC4A = function(buff) { | ||
RC4.prototype.codeBufferRC4A = function(buff) { | ||
var res = new Byffer(buff.length); | ||
var i = 0, j1 = 0, j2 = 0; | ||
var s1 = this.ksa(); | ||
var s2 = s1.slice(); | ||
for (var y = 0, l = buff.length; y < l; y++) { | ||
i = (i + 1) % 256; | ||
j1 = (j1 + s1[i]) % 256; | ||
s1[j1] = [s1[i],s1[i] = s1[j1]][0]; | ||
res[y] = buff[y] ^ s2[(s1[i] + s1[j1]) % 256]; | ||
y++; | ||
j2 = (j2 + s2[i]) % 256; | ||
s2[j2] = [s2[i],s2[i] = s2[j2]][0]; | ||
res[y] = buff[y] ^ s1[(s2[i] + s2[j1]) % 256]; | ||
} | ||
return res; | ||
return bodyRC4A(buff,new Buffer(buff.length),gKsa(this.key)); | ||
}; | ||
@@ -295,4 +360,4 @@ /** | ||
* @function codeRC4A | ||
* @param {String|Array} boh - data | ||
* @return {String|Array} | ||
* @param {String|Array|Buffer} boh - data | ||
* @return {String|Array|Buffer} | ||
*/ | ||
@@ -304,3 +369,3 @@ RC4.prototype.codeRC4A = function(boh) { | ||
} else if (Array.isArray(boh)) { | ||
return this.codeByteRC4A(boh); | ||
return this.codeArrayRC4A(boh); | ||
} else if (Buffer.isBuffer(boh)) { | ||
@@ -322,38 +387,14 @@ return this.codeBufferRC4A(boh); | ||
var res = ''; | ||
var i = 0, j = 0; | ||
var s = this.ksa(); | ||
var a = null, b = null; | ||
for (var y = 0, l = str.length; y < l; y++) { | ||
a = s[i]; | ||
j = s[(j + a) % 256]; | ||
b = s[j]; | ||
res += String.fromCharCode(str.charCodeAt(y) ^ s[s[b] + 1]); | ||
s[j] = [a,s[i] = b][0]; | ||
i = (i + 1) % 256; | ||
} | ||
return res; | ||
return bodyVMPC(str,'',gKsa(this.key)); | ||
}; | ||
/** | ||
* VMPC byte code | ||
* VMPC array code | ||
* | ||
* @function codeByteVMPC | ||
* @param {Array} byt - data | ||
* @function codeArrayVMPC | ||
* @param {Array} arr - data | ||
* @return {Array} | ||
*/ | ||
RC4.prototype.codeByteVMPC = function(byt) { | ||
RC4.prototype.codeArrayVMPC = function(arr) { | ||
var res = new Array(byt.length); | ||
var i = 0, j = 0; | ||
var s = this.ksa(); | ||
var a = null, b = null; | ||
for (var y = 0, l = byt.length; y < l; y++) { | ||
a = s[i]; | ||
j = s[(j + a) % 256]; | ||
b = s[j]; | ||
res[y] = byt[y] ^ s[s[b] + 1]; | ||
s[j] = [a,s[i] = b][0]; | ||
i = (i + 1) % 256; | ||
} | ||
return res; | ||
return bodyVMPC(arr,new Array(arr.length),gKsa(this.key)); | ||
}; | ||
@@ -369,15 +410,3 @@ /** | ||
var res = new Buffer(buff.length); | ||
var i = 0, j = 0; | ||
var s = this.ksa(); | ||
var a = null, b = null; | ||
for (var y = 0, l = buff.length; y < l; y++) { | ||
a = s[i]; | ||
j = s[(j + a) % 256]; | ||
b = s[j]; | ||
res[y] = buff[y] ^ s[s[b] + 1]; | ||
s[j] = [a,s[i] = b][0]; | ||
i = (i + 1) % 256; | ||
} | ||
return res; | ||
return bodyVMPC(buff,new Buffer(buff.length),gKsa(this.key)); | ||
}; | ||
@@ -396,3 +425,3 @@ /** | ||
} else if (Array.isArray(boh)) { | ||
return this.codeByteVMPC(boh); | ||
return this.codeArrayVMPC(boh); | ||
} else if (Buffer.isBuffer(boh)) { | ||
@@ -414,41 +443,14 @@ return this.codeBufferVMPC(boh); | ||
var res = ''; | ||
var i = 0, j = 0; | ||
var s = this.ksa(); | ||
var a = null, b = null, c = null; | ||
for (var y = 0, l = str.length; y < l; y++) { | ||
i = (i + 1) % 256; | ||
a = s[i]; | ||
j = s[(j + a) % 256]; | ||
b = s[j]; | ||
s[j] = [a,s[i] = b][0]; | ||
c = (s[i << 5 ^ j >> 3] + s[j << 5 ^ i >> 3]) % 256; | ||
res += String.fromCharCode(str.charCodeAt(y) ^ (s[a + b] + s[c ^ 0xAA]) | ||
^ s[j + b]); | ||
} | ||
return res; | ||
return bodyRC4p(str,'',gKsa(this.key)); | ||
}; | ||
/** | ||
* RC4p byte code | ||
* RC4p array code | ||
* | ||
* @function codeByteRC4p | ||
* @param {Array} byt - data | ||
* @function codeArrayRC4p | ||
* @param {Array} arr - data | ||
* @return {Array} | ||
*/ | ||
RC4.prototype.codeByteRC4p = function(byt) { | ||
RC4.prototype.codeArrayRC4p = function(arr) { | ||
var res = new Array(byt.length); | ||
var i = 0, j = 0; | ||
var s = this.ksa(); | ||
var a = null, b = null, c = null; | ||
for (var y = 0, l = byt.length; y < l; y++) { | ||
i = (i + 1) % 256; | ||
a = s[i]; | ||
j = s[(j + a) % 256]; | ||
b = s[j]; | ||
s[j] = [a,s[i] = b][0]; | ||
c = (s[i << 5 ^ j >> 3] + s[j << 5 ^ i >> 3]) % 256; | ||
res[y] = byt[y] ^ (s[a + b] + s[c ^ 0xAA]) ^ s[j + b]; | ||
} | ||
return res; | ||
return bodyRC4p(arr,new Array(arr.length),gKsa(this.key)); | ||
}; | ||
@@ -464,16 +466,3 @@ /** | ||
var res = new Buffer(buff.length); | ||
var i = 0, j = 0; | ||
var s = this.ksa(); | ||
var a = null, b = null, c = null; | ||
for (var y = 0, l = buff.length; y < l; y++) { | ||
i = (i + 1) % 256; | ||
a = s[i]; | ||
j = s[(j + a) % 256]; | ||
b = s[j]; | ||
s[j] = [a,s[i] = b][0]; | ||
c = (s[i << 5 ^ j >> 3] + s[j << 5 ^ i >> 3]) % 256; | ||
res[y] = buff[y] ^ (s[a + b] + s[c ^ 0xAA]) ^ s[j + b]; | ||
} | ||
return res; | ||
return bodyRC4p(buff,new Buffer(buff.length),gKsa(this.key)); | ||
}; | ||
@@ -484,4 +473,4 @@ /** | ||
* @function codeRC4p | ||
* @param {String|Array} boh - data | ||
* @return {String|Array} | ||
* @param {String|Array|Array} boh - data | ||
* @return {String|Array|Array} | ||
*/ | ||
@@ -493,3 +482,3 @@ RC4.prototype.codeRC4p = function(boh) { | ||
} else if (Array.isArray(boh)) { | ||
return this.codeByteRC4p(boh); | ||
return this.codeArrayRC4p(boh); | ||
} else if (Buffer.isBuffer(boh)) { | ||
@@ -496,0 +485,0 @@ return this.codeBufferRC4p(boh); |
{ | ||
"name": "arc4", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "rc4 stream cipher", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -40,8 +40,8 @@ #arc4 [![Build Status](https://travis-ci.org/hex7c0/arc4.svg?branch=master)](https://travis-ci.org/hex7c0/arc4) [![NPM version](https://badge.fury.io/js/arc4.svg)](http://badge.fury.io/js/arc4) | ||
encode byte data | ||
encode array data | ||
```js | ||
rc4.codeByte([49,50,51]); | ||
rc4.codeArray([49,50,51]); | ||
``` | ||
encode byte data | ||
encode buffer data | ||
```js | ||
@@ -73,3 +73,3 @@ rc4.codeBuffer(new Buffer('ciao')); | ||
- `param` - **String | Array** Your key *(default "throw Error")* | ||
- `param` - **String | Array | Buffer** Your key *(default "throw Error")* | ||
@@ -76,0 +76,0 @@ #### Examples |
49746
441