Skip to content

Commit ec4c050

Browse files
authored
Merge pull request #8 from crypto-browserify/safebuf
Safe Buffer & License
2 parents 11e46ea + 9345e1d commit ec4c050

File tree

6 files changed

+53
-21
lines changed

6 files changed

+53
-21
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 crypto-browserify contributors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

readme.md renamed to README.md

File renamed without changes.

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'
@@ -18,22 +18,24 @@ function CipherBase (hashMode) {
1818
this._decoder = null
1919
this._encoding = null
2020
}
21+
inherits(CipherBase, Transform)
22+
2123
CipherBase.prototype.update = function (data, inputEnc, outputEnc) {
2224
if (typeof data === 'string') {
23-
data = new Buffer(data, inputEnc)
25+
data = Buffer.from(data, inputEnc)
2426
}
27+
2528
var outData = this._update(data)
26-
if (this.hashMode) {
27-
return this
28-
}
29+
if (this.hashMode) return this
30+
2931
if (outputEnc) {
3032
outData = this._toString(outData, outputEnc)
3133
}
34+
3235
return outData
3336
}
3437

3538
CipherBase.prototype.setAutoPadding = function () {}
36-
3739
CipherBase.prototype.getAuthTag = function () {
3840
throw new Error('trying to get auth tag in unsupported state')
3941
}
@@ -66,9 +68,9 @@ CipherBase.prototype._flush = function (done) {
6668
this.push(this.__final())
6769
} catch (e) {
6870
err = e
69-
} finally {
70-
done(err)
7171
}
72+
73+
done(err)
7274
}
7375
CipherBase.prototype._finalOrDigest = function (outputEnc) {
7476
var outData = this.__final() || new Buffer('')
@@ -83,12 +85,15 @@ CipherBase.prototype._toString = function (value, enc, fin) {
8385
this._decoder = new StringDecoder(enc)
8486
this._encoding = enc
8587
}
86-
if (this._encoding !== enc) {
87-
throw new Error('can\'t switch encodings')
88-
}
88+
89+
if (this._encoding !== enc) throw new Error('can\'t switch encodings')
90+
8991
var out = this._decoder.write(value)
9092
if (fin) {
9193
out += this._decoder.end()
9294
}
95+
9396
return out
9497
}
98+
99+
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)