-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
161 lines (136 loc) · 4.06 KB
/
index.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const crypto = require('crypto');
const settings = {
saltLength: 8,
ivLength: 16,
pbkdf2: {
iterations: 10000,
keyLength: 32
},
hmac: {
length: 32
}
};
/**
*
* @name decrypt Decrypt a AES-256-CBC text encrypted
* @param {string} text Base64 String
* @param {string} password Passsword
*/
const decrypt = (text, password) => {
const { iv, cipherText, key } = extract(text, password);
const hexIv = Buffer.from(iv, 'hex');
const encryptedText = Buffer.from(cipherText, 'hex');
const decipher = crypto.createDecipheriv(
'aes-256-cbc',
Buffer.from(key),
hexIv
);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
};
/**
*
* @name encrypt Encrypt a text with AES-256-CBC
*
*
* Byte: | 0 | 1 | 2-9 | 10-17 | 18-33 | <- ... -> | n-32 - n |
*
* Contents: | version | options | encryptionSalt | HMACSalt | IV | ... ciphertext ... | HMAC |
*
*
* @param {string} text Base64 String
* @param {string} password Passsword
* @param {object} config
* @param {string} config.optionsFromEncryptedSource
* @param {string} config.version
* @param {string} config.options
* @param {string} config.salt
* @param {string} config.hmacSalt
* @param {string} config.iv
* @param {string} config.hmac
* @param {string} config.key
*/
const encrypt = (text, password, config = {}) => {
let source = {}
if (config.optionsFromEncryptedSource) {
source = extract(config.optionsFromEncryptedSource, password);
}
const { version, options, salt, hmacSalt, iv, hmac, key } = config.optionsFromEncryptedSource ? source : config
const cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(key), iv);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return Buffer.concat([version, options, salt, hmacSalt, iv, encrypted, hmac]).toString('base64');
};
/**
*
* @name extract Extract informations about AES-256-CBC from RNCryptor base64
* @param {string} text Base64 String
* @param {string} password Passsword
* @param {object} config Config behavor
* @param {boolean} config.hex If true return hexadecimal values
* @param {boolean} config.base64 If true return base64 values
*/
const extract = (text, password, config = {}) => {
let offset = 0;
const data = Buffer.from(text, 'base64');
const version = data.slice(offset, offset + 1);
offset += version.length;
const options = data.slice(offset, offset + 1);
offset += options.length;
const salt = data.slice(offset, offset + settings.saltLength);
offset += salt.length;
const hmacSalt = data.slice(offset, offset + settings.saltLength);
offset += hmacSalt.length;
const iv = data.slice(offset, offset + settings.ivLength);
offset += iv.length;
const hmac = data.slice(data.length - settings.hmac.length);
const cipherTextLength = data.length - offset - hmac.length;
const cipherText = data.slice(offset, offset + cipherTextLength);
const key = crypto.pbkdf2Sync(
password,
salt,
settings.pbkdf2.iterations,
settings.pbkdf2.keyLength,
'SHA1'
);
if (config.hex) {
return {
version: Buffer.from(version).toString('hex'),
options: Buffer.from(options).toString('hex'),
salt: Buffer.from(salt).toString('hex'),
hmacSalt: Buffer.from(hmacSalt).toString('hex'),
iv: Buffer.from(iv).toString('hex'),
hmac: Buffer.from(hmac).toString('hex'),
cipherText: Buffer.from(cipherText).toString('hex'),
key: Buffer.from(key).toString('hex')
}
}
if (config.base64) {
return {
version: Buffer.from(version).toString('base64'),
options: Buffer.from(options).toString('base64'),
salt: Buffer.from(salt).toString('base64'),
hmacSalt: Buffer.from(hmacSalt).toString('base64'),
iv: Buffer.from(iv).toString('base64'),
hmac: Buffer.from(hmac).toString('base64'),
cipherText: Buffer.from(cipherText).toString('base64'),
key: Buffer.from(key).toString('base64')
}
}
return {
version,
options,
salt,
hmacSalt,
iv,
hmac,
cipherText,
key
}
}
module.exports = {
decrypt,
encrypt,
extract
};