-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
219 lines (199 loc) · 5.58 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// aescrypt.js
// Dependencies
const crypto = require('crypto');
/**
* Creates a new AESCrypt helper that wraps the Node crypto Standard Library
* for `aes-256-cbc` encryption
* largest block size.
*
* @param {string} secret The secret key to use for the encryption/decryption
* @param {string} iv The initialization vector for the encryption/decryption
* @class AESCrypt
*/
class AESCrypt {
constructor(secret, iv) {
iv = iv || Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 0, 16);
this.algo = 'aes-256-cbc';
this.secret = Buffer.from(secret);
this.iv = Buffer.from(iv);
}
/**
* (Deprecated) Test a value set this this cipher encrypts and then decrypts successfully
*
* @param {*} data
* @return {*}
* @memberof AESCrypt
* @deprecated Please use `testiv`
*/
test(data) {
return this.testiv(data);
}
/**
* Test a value set with this cipher encrypts and then decrypts successfully
*
* @param {*} data
* @return {*}
* @memberof AESCrypt
*/
testiv(data) {
const dataBuffer = data instanceof Buffer ? data : Buffer.from(data);
return data.toString() === this.decryptiv(this.encryptiv(dataBuffer)).toString();
}
/**
* Slices a Buffer into an array of Buffers of max length `len`
*
* @param {Buffer} data Buffer data to chunk
* @param {integer} len The size of each buffer chunl
* @return {Buffer[]}
* @memberof AESCrypt
*/
getChunks(data, len) {
const dataBuffer = data instanceof Buffer ? data : Buffer.from(data);
const chunks = [];
for (let itr = 0, jtr = dataBuffer.length; itr < jtr; itr += len) {
chunks.push(dataBuffer.slice(itr, itr + len));
}
return chunks;
}
/**
* Return a cipher for aes-256-cbc encryption
*
* @param {*} [secret]
* @param {*} [iv]
* @return {Cipher}
* @memberof AESCrypt
*/
getCipher(secret, iv) {
return crypto.createCipheriv(this.algo, secret || this.secret, iv || this.iv);
}
/**
* Return a decipher for aes-256-cbc encryption
*
* @param {Buffer} [secret]
* @param {Buffer} [iv]
* @return {Decipher}
* @memberof AESCrypt
*/
getDecipher(secret, iv) {
return crypto.createDecipheriv(this.algo, secret || this.secret, iv || this.iv);
}
/**
* Perform aes-256-cbc encryption on a data block
*
* @param {Buffer} dataBuffer
* @param {Buffer|string} secret
* @param {Buffer|string} iv
* @return {Buffer}
* @memberof AESCrypt
*/
enc(dataBuffer, secret, iv) {
const cipher = this.getCipher(secret, iv);
let crypt = cipher.update(dataBuffer);
crypt += cipher.final('binary');
return Buffer.from(crypt, 'binary');
}
/**
* Perform aes-256-cbc decryption on a data block
*
* @param {Buffer} dataBuffer
* @param {Buffer|string} secret
* @param {Buffer|string} iv
* @return {Buffer}
* @memberof AESCrypt
*/
dec(dataBuffer, secret, iv) {
const decipher = this.getDecipher(secret, iv);
let decrypt = decipher.update(dataBuffer);
decrypt += decipher.final();
return Buffer.from(decrypt);
}
/**
* Chunks a buffer of data and encrypts each chunk and returns the combined buffer
*
* @param {Buffer} dataBuffer
* @param {Buffer|string} secret
* @param {Buffer|string} iv
* @return {Buffer}
* @memberof AESCrypt
*/
encData(dataBuffer, secret, iv) {
const out = [];
const chunks = this.getChunks(dataBuffer, 15);
for (let itr = 0, jtr = chunks.length; itr < jtr; itr++) {
out.push(this.enc(chunks[itr], secret, iv));
}
return Buffer.concat(out);
}
/**
* Chunks a buffer of encrypted data and decrypts each chunk and returns the combined buffer
*
* @param {Buffer} dataBuffer
* @param {Buffer|string} secret
* @param {Buffer|string} iv
* @return {Buffer}
* @memberof AESCrypt
*/
decData(dataBuffer, secret, iv) {
const out = [];
const chunks = this.getChunks(dataBuffer, 16);
for (let itr = 0, jtr = chunks.length; itr < jtr; itr++) {
out.push(this.dec(chunks[itr], secret, iv));
}
return Buffer.concat(out);
}
/**
* (Deprecated) Encrypts a value with the secret provided or the secret set on the instance
*
* @param {*} data
* @param {*} secret
* @return {*}
* @memberof AESCrypt
* @deprecated Please used `encryptiv`
*/
encrypt(data, secret) {
return this.encryptiv(data, secret || this.secret, this.iv);
}
/**
* Encrypts a value with the secret/iv provided or the secret/iv set on the instance
*
* @param {*} data
* @param {*} secret
* @param {*} iv
* @return {*}
* @memberof AESCrypt
*/
encryptiv(data, secret, iv) {
secret = secret || this.secret;
iv = iv || this.iv;
const dataBuffer = data instanceof Buffer ? data : Buffer.from(data);
return this.encData(dataBuffer, secret, iv);
}
/**
* (Deprecated) Decrypts a value with the secret provided or the secret set on the instance
*
* @param {*} data
* @param {*} secret
* @return {*}
* @memberof AESCrypt
* @deprecated Please use `decryptiv`
*/
decrypt(data, secret) {
return this.decryptiv(data, secret || this.secret, this.iv);
}
/**
* Decrypts a value with the secret/iv provided or the secret/iv set on the instance*
*
* @param {*} data
* @param {*} secret
* @param {*} iv
* @return {*}
* @memberof AESCrypt
*/
decryptiv(data, secret, iv) {
secret = secret || this.secret;
iv = iv || this.iv;
const dataBuffer = data instanceof Buffer ? data : Buffer.from(data);
return this.decData(dataBuffer, secret, iv);
}
}
module.exports = AESCrypt;