UNPKG

1.02 kBJavaScriptView Raw
1/**
2 * Aescbc (experimental)
3 * =====================
4 *
5 * This is a convenience class for using Aes with Cbc. This is a low-level tool
6 * that does not include authentication. You should only use this if you are
7 * authenticating your data somehow else.
8 */
9'use strict'
10
11import { Aes } from './aes'
12import { Cbc } from './cbc'
13import { Random } from './random'
14
15class Aescbc { }
16
17Aescbc.encrypt = function (messageBuf, cipherKeyBuf, ivBuf, concatIvBuf = true) {
18 ivBuf = ivBuf || Random.getRandomBuffer(128 / 8)
19 const ctBuf = Cbc.encrypt(messageBuf, ivBuf, Aes, cipherKeyBuf)
20 if (concatIvBuf) {
21 return Buffer.concat([ivBuf, ctBuf])
22 } else {
23 return ctBuf
24 }
25}
26
27Aescbc.decrypt = function (encBuf, cipherKeyBuf, ivBuf = false) {
28 if (!ivBuf) {
29 const ivBuf = encBuf.slice(0, 128 / 8)
30 const ctBuf = encBuf.slice(128 / 8)
31 return Cbc.decrypt(ctBuf, ivBuf, Aes, cipherKeyBuf)
32 } else {
33 const ctBuf = encBuf
34 return Cbc.decrypt(ctBuf, ivBuf, Aes, cipherKeyBuf)
35 }
36}
37
38export { Aescbc }