forked from jbenet/js-senc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes.js
35 lines (28 loc) · 742 Bytes
/
aes.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
exports.KeyLength = 32 // aes256
exports.BlockSize = 16
exports.RandomKey = function() {
return crypto.randomBytes(KeyLength)
}
// this file is here because we need a sync create call.
const ciphers = require('libp2p-crypto/src/aes/ciphers')
const CIPHER_MODES = {
16: 'aes-128-ctr',
32: 'aes-256-ctr'
}
exports.create = function (key, iv) {
const mode = CIPHER_MODES[key.length]
if (!mode) {
throw new Error('Invalid key length')
}
const cipher = ciphers.createCipheriv(mode, key, iv)
const decipher = ciphers.createDecipheriv(mode, key, iv)
const res = {
encrypt (data, cb) {
cb(null, cipher.update(data))
},
decrypt (data, cb) {
cb(null, decipher.update(data))
}
}
return res
}