-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic-crypto.js
49 lines (43 loc) · 1.7 KB
/
static-crypto.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
44
45
46
47
48
const crypto = require('crypto');
const algorithm = 'aes-256-ctr';
function static_hash(x, outputLength) {
var salt = crypto.createHash('md5').update(x).digest("hex");
return crypto.pbkdf2Sync(x, salt, 100000, outputLength, 'sha512');
}
function generate_from(password) {
const key_iv = static_hash(password, 32 + 16);
return {
key: key_iv.slice(0, 32),
iv: key_iv.slice(32),
salt: key_iv.slice(16)
};
}
exports.one_way = function (plain_text){
let generated = generate_from(plain_text)
let encryption_key = generated.salt
let iv = generated.iv
let cipher = crypto.createCipheriv(algorithm, Buffer.from(encryption_key, 'hex'), iv);
let encrypted = cipher.update(plain_text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return encrypted.toString('hex');
}
exports.encrypt = function (passwd, plain_text) {
let generated = generate_from(passwd)
let encryption_key = generated.key
let iv = generated.iv
let cipher = crypto.createCipheriv(algorithm, Buffer.from(encryption_key, 'hex'), iv);
let encrypted = cipher.update(plain_text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
//return iv.toString('hex') + ':' + encrypted.toString('hex');
return encrypted.toString('hex');
}
exports.decrypt = function (passwd, chipertext) {
let generated = generate_from(passwd)
let encryption_key = generated.key
let iv = generated.iv
let encryptedText = Buffer.from(chipertext, 'hex')
let decipher = crypto.createDecipheriv(algorithm, Buffer.from(encryption_key, 'hex'), iv);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
}