1
|
var CipherBase = require('cipher-base')
|
2
|
var des = require('des.js')
|
3
|
var inherits = require('inherits')
|
4
|
var Buffer = require('safe-buffer').Buffer
|
5
|
|
6
|
var modes = {
|
7
|
'des-ede3-cbc': des.CBC.instantiate(des.EDE),
|
8
|
'des-ede3': des.EDE,
|
9
|
'des-ede-cbc': des.CBC.instantiate(des.EDE),
|
10
|
'des-ede': des.EDE,
|
11
|
'des-cbc': des.CBC.instantiate(des.DES),
|
12
|
'des-ecb': des.DES
|
13
|
}
|
14
|
modes.des = modes['des-cbc']
|
15
|
modes.des3 = modes['des-ede3-cbc']
|
16
|
module.exports = DES
|
17
|
inherits(DES, CipherBase)
|
18
|
function DES (opts) {
|
19
|
CipherBase.call(this)
|
20
|
var modeName = opts.mode.toLowerCase()
|
21
|
var mode = modes[modeName]
|
22
|
var type
|
23
|
if (opts.decrypt) {
|
24
|
type = 'decrypt'
|
25
|
} else {
|
26
|
type = 'encrypt'
|
27
|
}
|
28
|
var key = opts.key
|
29
|
if (!Buffer.isBuffer(key)) {
|
30
|
key = Buffer.from(key)
|
31
|
}
|
32
|
if (modeName === 'des-ede' || modeName === 'des-ede-cbc') {
|
33
|
key = Buffer.concat([key, key.slice(0, 8)])
|
34
|
}
|
35
|
var iv = opts.iv
|
36
|
if (!Buffer.isBuffer(iv)) {
|
37
|
iv = Buffer.from(iv)
|
38
|
}
|
39
|
this._des = mode.create({
|
40
|
key: key,
|
41
|
iv: iv,
|
42
|
type: type
|
43
|
})
|
44
|
}
|
45
|
DES.prototype._update = function (data) {
|
46
|
return Buffer.from(this._des.update(data))
|
47
|
}
|
48
|
DES.prototype._final = function () {
|
49
|
return Buffer.from(this._des.final())
|
50
|
}
|