-
Notifications
You must be signed in to change notification settings - Fork 90
/
rc_4_sha256.js
43 lines (40 loc) · 1.14 KB
/
rc_4_sha256.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'use strict';
const path = require('path');
var CryptoJS = require(path.join(
window.antSword.remote.process.env.AS_WORKDIR, 'node_modules/crypto-js'
));
function rc4(key, data) {
let pwd = key;
let cipher = '';
key = [];
let box = [];
let pwd_length = pwd.length;
let data_length = data.length;
for (var i = 0; i < 256; i++) {
key[i] = pwd[i % pwd_length].charCodeAt();
box[i] = i;
}
for (var j = i = 0; i < 256; i++) {
j = (j + box[i] + key[i]) % 256;
var tmp = box[i];
box[i] = box[j];
box[j] = tmp;
}
for (var a = j = i = 0; i < data_length; i++) {
a = (a + 1) % 256;
j = (j + box[a]) % 256;
tmp = box[a];
box[a] = box[j];
box[j] = tmp;
let k = box[((box[a] + box[j]) % 256)];
cipher += String.fromCharCode(data[i].charCodeAt() ^ k);
}
return cipher;
}
module.exports = (pwd, data) => {
let str = Buffer.from(data['_']).toString();
let key = CryptoJS.SHA256(pwd).toString();
data[pwd] = Buffer.from(rc4(key, str), 'binary').toString('base64');
delete data['_'];
return data;
}