Skip to content

Commit 193bbeb

Browse files
committed
use safe-buffer
1 parent de2af97 commit 193bbeb

File tree

3 files changed

+31
-21
lines changed

3 files changed

+31
-21
lines changed

index.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
var Buffer = require('safe-buffer').Buffer
12
var Transform = require('stream').Transform
2-
var inherits = require('inherits')
33
var StringDecoder = require('string_decoder').StringDecoder
4-
module.exports = CipherBase
5-
inherits(CipherBase, Transform)
4+
var inherits = require('inherits')
5+
66
function CipherBase (hashMode) {
77
Transform.call(this)
88
this.hashMode = typeof hashMode === 'string'
@@ -14,22 +14,24 @@ function CipherBase (hashMode) {
1414
this._decoder = null
1515
this._encoding = null
1616
}
17+
inherits(CipherBase, Transform)
18+
1719
CipherBase.prototype.update = function (data, inputEnc, outputEnc) {
1820
if (typeof data === 'string') {
19-
data = new Buffer(data, inputEnc)
21+
data = Buffer.from(data, inputEnc)
2022
}
23+
2124
var outData = this._update(data)
22-
if (this.hashMode) {
23-
return this
24-
}
25+
if (this.hashMode) return this
26+
2527
if (outputEnc) {
2628
outData = this._toString(outData, outputEnc)
2729
}
30+
2831
return outData
2932
}
3033

3134
CipherBase.prototype.setAutoPadding = function () {}
32-
3335
CipherBase.prototype.getAuthTag = function () {
3436
throw new Error('trying to get auth tag in unsupported state')
3537
}
@@ -62,9 +64,9 @@ CipherBase.prototype._flush = function (done) {
6264
this.push(this._final())
6365
} catch (e) {
6466
err = e
65-
} finally {
66-
done(err)
6767
}
68+
69+
done(err)
6870
}
6971
CipherBase.prototype._finalOrDigest = function (outputEnc) {
7072
var outData = this._final() || new Buffer('')
@@ -79,12 +81,15 @@ CipherBase.prototype._toString = function (value, enc, fin) {
7981
this._decoder = new StringDecoder(enc)
8082
this._encoding = enc
8183
}
82-
if (this._encoding !== enc) {
83-
throw new Error('can\'t switch encodings')
84-
}
84+
85+
if (this._encoding !== enc) throw new Error('can\'t switch encodings')
86+
8587
var out = this._decoder.write(value)
8688
if (fin) {
8789
out += this._decoder.end()
8890
}
91+
8992
return out
9093
}
94+
95+
module.exports = CipherBase

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
},
2222
"homepage": "https://github.com/crypto-browserify/cipher-base#readme",
2323
"dependencies": {
24-
"inherits": "^2.0.1"
24+
"inherits": "^2.0.1",
25+
"safe-buffer": "^5.0.1"
2526
},
2627
"devDependencies": {
28+
"standard": "^10.0.2",
2729
"tap-spec": "^4.1.0",
2830
"tape": "^4.2.0"
2931
}

test.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
var test = require('tape')
1+
var Buffer = require('safe-buffer').Buffer
22
var CipherBase = require('./')
3+
4+
var test = require('tape')
35
var inherits = require('inherits')
46

57
test('basic version', function (t) {
6-
inherits(Cipher, CipherBase)
78
function Cipher () {
89
CipherBase.call(this)
910
}
11+
inherits(Cipher, CipherBase)
1012
Cipher.prototype._update = function (input) {
1113
t.ok(Buffer.isBuffer(input))
1214
return input
@@ -17,16 +19,16 @@ test('basic version', function (t) {
1719
var cipher = new Cipher()
1820
var utf8 = 'abc123abcd'
1921
var update = cipher.update(utf8, 'utf8', 'base64') + cipher.final('base64')
20-
var string = (new Buffer(update, 'base64')).toString()
22+
var string = (Buffer.from(update, 'base64')).toString()
2123
t.equals(utf8, string)
2224
t.end()
2325
})
2426
test('hash mode', function (t) {
25-
inherits(Cipher, CipherBase)
2627
function Cipher () {
2728
CipherBase.call(this, 'finalName')
2829
this._cache = []
2930
}
31+
inherits(Cipher, CipherBase)
3032
Cipher.prototype._update = function (input) {
3133
t.ok(Buffer.isBuffer(input))
3234
this._cache.push(input)
@@ -37,17 +39,17 @@ test('hash mode', function (t) {
3739
var cipher = new Cipher()
3840
var utf8 = 'abc123abcd'
3941
var update = cipher.update(utf8, 'utf8').finalName('base64')
40-
var string = (new Buffer(update, 'base64')).toString()
42+
var string = (Buffer.from(update, 'base64')).toString()
4143

4244
t.equals(utf8, string)
4345
t.end()
4446
})
4547
test('hash mode as stream', function (t) {
46-
inherits(Cipher, CipherBase)
4748
function Cipher () {
4849
CipherBase.call(this, 'finalName')
4950
this._cache = []
5051
}
52+
inherits(Cipher, CipherBase)
5153
Cipher.prototype._update = function (input) {
5254
t.ok(Buffer.isBuffer(input))
5355
this._cache.push(input)
@@ -62,11 +64,12 @@ test('hash mode as stream', function (t) {
6264
var utf8 = 'abc123abcd'
6365
cipher.end(utf8, 'utf8')
6466
var update = cipher.read().toString('base64')
65-
var string = (new Buffer(update, 'base64')).toString()
67+
var string = (Buffer.from(update, 'base64')).toString()
6668

6769
t.equals(utf8, string)
6870
t.end()
6971
})
72+
7073
test('encodings', function (t) {
7174
inherits(Cipher, CipherBase)
7275
function Cipher () {

0 commit comments

Comments
 (0)