1
|
var DES = require('browserify-des')
|
2
|
var aes = require('browserify-aes/browser')
|
3
|
var aesModes = require('browserify-aes/modes')
|
4
|
var desModes = require('browserify-des/modes')
|
5
|
var ebtk = require('evp_bytestokey')
|
6
|
|
7
|
function createCipher (suite, password) {
|
8
|
suite = suite.toLowerCase()
|
9
|
|
10
|
var keyLen, ivLen
|
11
|
if (aesModes[suite]) {
|
12
|
keyLen = aesModes[suite].key
|
13
|
ivLen = aesModes[suite].iv
|
14
|
} else if (desModes[suite]) {
|
15
|
keyLen = desModes[suite].key * 8
|
16
|
ivLen = desModes[suite].iv
|
17
|
} else {
|
18
|
throw new TypeError('invalid suite type')
|
19
|
}
|
20
|
|
21
|
var keys = ebtk(password, false, keyLen, ivLen)
|
22
|
return createCipheriv(suite, keys.key, keys.iv)
|
23
|
}
|
24
|
|
25
|
function createDecipher (suite, password) {
|
26
|
suite = suite.toLowerCase()
|
27
|
|
28
|
var keyLen, ivLen
|
29
|
if (aesModes[suite]) {
|
30
|
keyLen = aesModes[suite].key
|
31
|
ivLen = aesModes[suite].iv
|
32
|
} else if (desModes[suite]) {
|
33
|
keyLen = desModes[suite].key * 8
|
34
|
ivLen = desModes[suite].iv
|
35
|
} else {
|
36
|
throw new TypeError('invalid suite type')
|
37
|
}
|
38
|
|
39
|
var keys = ebtk(password, false, keyLen, ivLen)
|
40
|
return createDecipheriv(suite, keys.key, keys.iv)
|
41
|
}
|
42
|
|
43
|
function createCipheriv (suite, key, iv) {
|
44
|
suite = suite.toLowerCase()
|
45
|
if (aesModes[suite]) return aes.createCipheriv(suite, key, iv)
|
46
|
if (desModes[suite]) return new DES({ key: key, iv: iv, mode: suite })
|
47
|
|
48
|
throw new TypeError('invalid suite type')
|
49
|
}
|
50
|
|
51
|
function createDecipheriv (suite, key, iv) {
|
52
|
suite = suite.toLowerCase()
|
53
|
if (aesModes[suite]) return aes.createDecipheriv(suite, key, iv)
|
54
|
if (desModes[suite]) return new DES({ key: key, iv: iv, mode: suite, decrypt: true })
|
55
|
|
56
|
throw new TypeError('invalid suite type')
|
57
|
}
|
58
|
|
59
|
function getCiphers () {
|
60
|
return Object.keys(desModes).concat(aes.getCiphers())
|
61
|
}
|
62
|
|
63
|
exports.createCipher = exports.Cipher = createCipher
|
64
|
exports.createCipheriv = exports.Cipheriv = createCipheriv
|
65
|
exports.createDecipher = exports.Decipher = createDecipher
|
66
|
exports.createDecipheriv = exports.Decipheriv = createDecipheriv
|
67
|
exports.listCiphers = exports.getCiphers = getCiphers
|