-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquickstart.js
56 lines (36 loc) · 1.42 KB
/
quickstart.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
49
50
51
52
var aesjs = require('aes-js');
var crypto = require('crypto');
module.exports = {
encrypt: function(secret, data) {
function str2byte(str) {
uint = new Uint8Array(str.length);
for (var i = 0, j = str.length; i < j; ++i) {
uint[i] = str.charCodeAt(i);
}
return uint;
}
let secret_password = str2byte(crypto.createHash('md5').update(secret, 'utf-8').digest('hex').toUpperCase());
var textBytes = aesjs.utils.utf8.toBytes(data);
var aesCbc = new aesjs.ModeOfOperation.ctr(secret_password);
var encryptedBytes = aesCbc.encrypt(textBytes);
var encryptedHex = aesjs.utils.hex.fromBytes(encryptedBytes);
return encryptedHex;
},
decrypt: function(secret, password) {
function str2byte(str) {
uint = new Uint8Array(str.length);
for (var i = 0, j = str.length; i < j; ++i) {
uint[i] = str.charCodeAt(i);
}
return uint;
}
var encryptedBytes = aesjs.utils.hex.toBytes(password);
let secret_password = crypto.createHash('md5').update(secret, 'utf-8').digest('hex').toUpperCase();
let secret_password_vector = str2byte(secret_password);
var aesCtr = new aesjs.ModeOfOperation.ctr(secret_password_vector);
var decryptedBytes = aesCtr.decrypt(encryptedBytes);
// Convert our bytes back into text
var decryptedText = aesjs.utils.utf8.fromBytes(decryptedBytes);
return decryptedText;
}
}