|
1 | | -var Buffer = require('safe-buffer').Buffer |
2 | | -var Transform = require('stream').Transform |
3 | | -var StringDecoder = require('string_decoder').StringDecoder |
4 | | -var inherits = require('inherits') |
5 | | - |
6 | | -function CipherBase (hashMode) { |
7 | | - Transform.call(this) |
8 | | - this.hashMode = typeof hashMode === 'string' |
9 | | - if (this.hashMode) { |
10 | | - this[hashMode] = this._finalOrDigest |
11 | | - } else { |
12 | | - this.final = this._finalOrDigest |
13 | | - } |
14 | | - if (this._final) { |
15 | | - this.__final = this._final |
16 | | - this._final = null |
17 | | - } |
18 | | - this._decoder = null |
19 | | - this._encoding = null |
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var Buffer = require('safe-buffer').Buffer; |
| 4 | +var Transform = require('stream').Transform; |
| 5 | +var StringDecoder = require('string_decoder').StringDecoder; |
| 6 | +var inherits = require('inherits'); |
| 7 | + |
| 8 | +function CipherBase(hashMode) { |
| 9 | + Transform.call(this); |
| 10 | + this.hashMode = typeof hashMode === 'string'; |
| 11 | + if (this.hashMode) { |
| 12 | + this[hashMode] = this._finalOrDigest; |
| 13 | + } else { |
| 14 | + this['final'] = this._finalOrDigest; |
| 15 | + } |
| 16 | + if (this._final) { |
| 17 | + this.__final = this._final; |
| 18 | + this._final = null; |
| 19 | + } |
| 20 | + this._decoder = null; |
| 21 | + this._encoding = null; |
20 | 22 | } |
21 | | -inherits(CipherBase, Transform) |
| 23 | +inherits(CipherBase, Transform); |
22 | 24 |
|
23 | 25 | CipherBase.prototype.update = function (data, inputEnc, outputEnc) { |
24 | | - if (typeof data === 'string') { |
25 | | - data = Buffer.from(data, inputEnc) |
26 | | - } |
| 26 | + var bufferData = typeof data === 'string' ? Buffer.from(data, inputEnc) : data; |
27 | 27 |
|
28 | | - var outData = this._update(data) |
29 | | - if (this.hashMode) return this |
| 28 | + var outData = this._update(bufferData); |
| 29 | + if (this.hashMode) { |
| 30 | + return this; |
| 31 | + } |
30 | 32 |
|
31 | | - if (outputEnc) { |
32 | | - outData = this._toString(outData, outputEnc) |
33 | | - } |
| 33 | + if (outputEnc) { |
| 34 | + outData = this._toString(outData, outputEnc); |
| 35 | + } |
34 | 36 |
|
35 | | - return outData |
36 | | -} |
| 37 | + return outData; |
| 38 | +}; |
37 | 39 |
|
38 | | -CipherBase.prototype.setAutoPadding = function () {} |
| 40 | +CipherBase.prototype.setAutoPadding = function () {}; |
39 | 41 | CipherBase.prototype.getAuthTag = function () { |
40 | | - throw new Error('trying to get auth tag in unsupported state') |
41 | | -} |
| 42 | + throw new Error('trying to get auth tag in unsupported state'); |
| 43 | +}; |
42 | 44 |
|
43 | 45 | CipherBase.prototype.setAuthTag = function () { |
44 | | - throw new Error('trying to set auth tag in unsupported state') |
45 | | -} |
| 46 | + throw new Error('trying to set auth tag in unsupported state'); |
| 47 | +}; |
46 | 48 |
|
47 | 49 | CipherBase.prototype.setAAD = function () { |
48 | | - throw new Error('trying to set aad in unsupported state') |
49 | | -} |
| 50 | + throw new Error('trying to set aad in unsupported state'); |
| 51 | +}; |
50 | 52 |
|
51 | 53 | CipherBase.prototype._transform = function (data, _, next) { |
52 | | - var err |
53 | | - try { |
54 | | - if (this.hashMode) { |
55 | | - this._update(data) |
56 | | - } else { |
57 | | - this.push(this._update(data)) |
58 | | - } |
59 | | - } catch (e) { |
60 | | - err = e |
61 | | - } finally { |
62 | | - next(err) |
63 | | - } |
64 | | -} |
| 54 | + var err; |
| 55 | + try { |
| 56 | + if (this.hashMode) { |
| 57 | + this._update(data); |
| 58 | + } else { |
| 59 | + this.push(this._update(data)); |
| 60 | + } |
| 61 | + } catch (e) { |
| 62 | + err = e; |
| 63 | + } finally { |
| 64 | + next(err); |
| 65 | + } |
| 66 | +}; |
65 | 67 | CipherBase.prototype._flush = function (done) { |
66 | | - var err |
67 | | - try { |
68 | | - this.push(this.__final()) |
69 | | - } catch (e) { |
70 | | - err = e |
71 | | - } |
72 | | - |
73 | | - done(err) |
74 | | -} |
| 68 | + var err; |
| 69 | + try { |
| 70 | + this.push(this.__final()); |
| 71 | + } catch (e) { |
| 72 | + err = e; |
| 73 | + } |
| 74 | + |
| 75 | + done(err); |
| 76 | +}; |
75 | 77 | CipherBase.prototype._finalOrDigest = function (outputEnc) { |
76 | | - var outData = this.__final() || Buffer.alloc(0) |
77 | | - if (outputEnc) { |
78 | | - outData = this._toString(outData, outputEnc, true) |
79 | | - } |
80 | | - return outData |
81 | | -} |
| 78 | + var outData = this.__final() || Buffer.alloc(0); |
| 79 | + if (outputEnc) { |
| 80 | + outData = this._toString(outData, outputEnc, true); |
| 81 | + } |
| 82 | + return outData; |
| 83 | +}; |
82 | 84 |
|
83 | 85 | CipherBase.prototype._toString = function (value, enc, fin) { |
84 | | - if (!this._decoder) { |
85 | | - this._decoder = new StringDecoder(enc) |
86 | | - this._encoding = enc |
87 | | - } |
| 86 | + if (!this._decoder) { |
| 87 | + this._decoder = new StringDecoder(enc); |
| 88 | + this._encoding = enc; |
| 89 | + } |
88 | 90 |
|
89 | | - if (this._encoding !== enc) throw new Error('can\'t switch encodings') |
| 91 | + if (this._encoding !== enc) { |
| 92 | + throw new Error('can’t switch encodings'); |
| 93 | + } |
90 | 94 |
|
91 | | - var out = this._decoder.write(value) |
92 | | - if (fin) { |
93 | | - out += this._decoder.end() |
94 | | - } |
| 95 | + var out = this._decoder.write(value); |
| 96 | + if (fin) { |
| 97 | + out += this._decoder.end(); |
| 98 | + } |
95 | 99 |
|
96 | | - return out |
97 | | -} |
| 100 | + return out; |
| 101 | +}; |
98 | 102 |
|
99 | | -module.exports = CipherBase |
| 103 | +module.exports = CipherBase; |
0 commit comments