Skip to content

Commit 92b2b83

Browse files
authored
update deps, modernise usage, use readable-stream (#49)
* use readable-stream instead of bare stream * update deps, fix most lint errors * switch from `new Buffer()` and require 'buffer' explicitly * only test signature algorithms supported by current Node.js * update travis for current Node.js versions
1 parent 44a10f6 commit 92b2b83

File tree

6 files changed

+63
-61
lines changed

6 files changed

+63
-61
lines changed

.travis.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
sudo: false
22
language: node_js
33
node_js:
4-
- "0.11"
5-
- "0.12"
6-
- "4"
7-
- "5"
8-
- "6"
9-
- "7"
4+
- "8"
5+
- "10"
6+
- "12"
7+
- "14"
8+
- lts/*
9+
- current
1010
matrix:
1111
include:
12-
- node_js: "4"
12+
- node_js: "12"
1313
env: TEST_SUITE=lint
1414
env:
1515
- TEST_SUITE=unit

browser/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
var Buffer = require('buffer').Buffer
12
var createHash = require('create-hash')
2-
var stream = require('stream')
3+
var stream = require('readable-stream')
34
var inherits = require('inherits')
45
var sign = require('./sign')
56
var verify = require('./verify')
67

78
var algorithms = require('./algorithms.json')
89
Object.keys(algorithms).forEach(function (key) {
9-
algorithms[key].id = new Buffer(algorithms[key].id, 'hex')
10+
algorithms[key].id = Buffer.from(algorithms[key].id, 'hex')
1011
algorithms[key.toLowerCase()] = algorithms[key]
1112
})
1213

@@ -29,7 +30,7 @@ Sign.prototype._write = function _write (data, _, done) {
2930
}
3031

3132
Sign.prototype.update = function update (data, enc) {
32-
if (typeof data === 'string') data = new Buffer(data, enc)
33+
if (typeof data === 'string') data = Buffer.from(data, enc)
3334

3435
this._hash.update(data)
3536
return this
@@ -61,14 +62,14 @@ Verify.prototype._write = function _write (data, _, done) {
6162
}
6263

6364
Verify.prototype.update = function update (data, enc) {
64-
if (typeof data === 'string') data = new Buffer(data, enc)
65+
if (typeof data === 'string') data = Buffer.from(data, enc)
6566

6667
this._hash.update(data)
6768
return this
6869
}
6970

7071
Verify.prototype.verify = function verifyMethod (key, sig, enc) {
71-
if (typeof sig === 'string') sig = new Buffer(sig, enc)
72+
if (typeof sig === 'string') sig = Buffer.from(sig, enc)
7273

7374
this.end()
7475
var hash = this._hash.digest()

browser/sign.js

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// much of this based on https://github.com/indutny/self-signed/blob/gh-pages/lib/rsa.js
2+
var Buffer = require('buffer').Buffer
23
var createHmac = require('create-hmac')
34
var crt = require('browserify-rsa')
45
var EC = require('elliptic').ec
@@ -20,7 +21,7 @@ function sign (hash, key, hashType, signType, tag) {
2021
}
2122
hash = Buffer.concat([tag, hash])
2223
var len = priv.modulus.byteLength()
23-
var pad = [ 0, 1 ]
24+
var pad = [0, 1]
2425
while (hash.length + pad.length + 1 < len) pad.push(0xff)
2526
pad.push(0x00)
2627
var i = -1
@@ -38,7 +39,7 @@ function ecSign (hash, priv) {
3839
var key = curve.keyFromPrivate(priv.privateKey)
3940
var out = key.sign(hash)
4041

41-
return new Buffer(out.toDER())
42+
return Buffer.from(out.toDER())
4243
}
4344

4445
function dsaSign (hash, priv, algo) {
@@ -68,31 +69,29 @@ function toDER (r, s) {
6869
s = s.toArray()
6970

7071
// Pad values
71-
if (r[0] & 0x80) r = [ 0 ].concat(r)
72-
if (s[0] & 0x80) s = [ 0 ].concat(s)
72+
if (r[0] & 0x80) r = [0].concat(r)
73+
if (s[0] & 0x80) s = [0].concat(s)
7374

7475
var total = r.length + s.length + 4
75-
var res = [ 0x30, total, 0x02, r.length ]
76-
res = res.concat(r, [ 0x02, s.length ], s)
77-
return new Buffer(res)
76+
var res = [0x30, total, 0x02, r.length]
77+
res = res.concat(r, [0x02, s.length], s)
78+
return Buffer.from(res)
7879
}
7980

8081
function getKey (x, q, hash, algo) {
81-
x = new Buffer(x.toArray())
82+
x = Buffer.from(x.toArray())
8283
if (x.length < q.byteLength()) {
83-
var zeros = new Buffer(q.byteLength() - x.length)
84-
zeros.fill(0)
85-
x = Buffer.concat([ zeros, x ])
84+
var zeros = Buffer.alloc(q.byteLength() - x.length)
85+
x = Buffer.concat([zeros, x])
8686
}
8787
var hlen = hash.length
8888
var hbits = bits2octets(hash, q)
89-
var v = new Buffer(hlen)
89+
var v = Buffer.alloc(hlen)
9090
v.fill(1)
91-
var k = new Buffer(hlen)
92-
k.fill(0)
93-
k = createHmac(algo, k).update(v).update(new Buffer([ 0 ])).update(x).update(hbits).digest()
91+
var k = Buffer.alloc(hlen)
92+
k = createHmac(algo, k).update(v).update(Buffer.from([0])).update(x).update(hbits).digest()
9493
v = createHmac(algo, k).update(v).digest()
95-
k = createHmac(algo, k).update(v).update(new Buffer([ 1 ])).update(x).update(hbits).digest()
94+
k = createHmac(algo, k).update(v).update(Buffer.from([1])).update(x).update(hbits).digest()
9695
v = createHmac(algo, k).update(v).digest()
9796
return { k: k, v: v }
9897
}
@@ -107,11 +106,10 @@ function bits2int (obits, q) {
107106
function bits2octets (bits, q) {
108107
bits = bits2int(bits, q)
109108
bits = bits.mod(q)
110-
var out = new Buffer(bits.toArray())
109+
var out = Buffer.from(bits.toArray())
111110
if (out.length < q.byteLength()) {
112-
var zeros = new Buffer(q.byteLength() - out.length)
113-
zeros.fill(0)
114-
out = Buffer.concat([ zeros, out ])
111+
var zeros = Buffer.alloc(q.byteLength() - out.length)
112+
out = Buffer.concat([zeros, out])
115113
}
116114
return out
117115
}
@@ -121,15 +119,15 @@ function makeKey (q, kv, algo) {
121119
var k
122120

123121
do {
124-
t = new Buffer(0)
122+
t = Buffer.alloc(0)
125123

126124
while (t.length * 8 < q.bitLength()) {
127125
kv.v = createHmac(algo, kv.k).update(kv.v).digest()
128-
t = Buffer.concat([ t, kv.v ])
126+
t = Buffer.concat([t, kv.v])
129127
}
130128

131129
k = bits2int(t, q)
132-
kv.k = createHmac(algo, kv.k).update(kv.v).update(new Buffer([ 0 ])).digest()
130+
kv.k = createHmac(algo, kv.k).update(kv.v).update(Buffer.from([0])).digest()
133131
kv.v = createHmac(algo, kv.k).update(kv.v).digest()
134132
} while (k.cmp(q) !== -1)
135133

browser/verify.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// much of this based on https://github.com/indutny/self-signed/blob/gh-pages/lib/rsa.js
2+
var Buffer = require('buffer').Buffer
23
var BN = require('bn.js')
34
var EC = require('elliptic').ec
45
var parseKeys = require('parse-asn1')
@@ -18,7 +19,7 @@ function verify (sig, hash, key, signType, tag) {
1819
}
1920
hash = Buffer.concat([tag, hash])
2021
var len = pub.modulus.byteLength()
21-
var pad = [ 1 ]
22+
var pad = [1]
2223
var padNum = 0
2324
while (hash.length + pad.length + 2 < len) {
2425
pad.push(0xff)
@@ -29,12 +30,12 @@ function verify (sig, hash, key, signType, tag) {
2930
while (++i < hash.length) {
3031
pad.push(hash[i])
3132
}
32-
pad = new Buffer(pad)
33+
pad = Buffer.from(pad)
3334
var red = BN.mont(pub.modulus)
3435
sig = new BN(sig).toRed(red)
3536

3637
sig = sig.redPow(new BN(pub.publicExponent))
37-
sig = new Buffer(sig.fromRed().toArray())
38+
sig = Buffer.from(sig.fromRed().toArray())
3839
var out = padNum < 8 ? 1 : 0
3940
len = Math.min(sig.length, pad.length)
4041
if (sig.length !== pad.length) out = 1

package.json

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,19 @@
2323
"unit": "tape test/*.js"
2424
},
2525
"dependencies": {
26-
"bn.js": "^4.1.1",
27-
"browserify-rsa": "^4.0.0",
28-
"create-hash": "^1.1.0",
29-
"create-hmac": "^1.1.2",
30-
"elliptic": "^6.0.0",
31-
"inherits": "^2.0.1",
32-
"parse-asn1": "^5.0.0"
26+
"bn.js": "^5.1.1",
27+
"browserify-rsa": "^4.0.1",
28+
"create-hash": "^1.2.0",
29+
"create-hmac": "^1.1.7",
30+
"elliptic": "^6.5.2",
31+
"inherits": "^2.0.4",
32+
"parse-asn1": "^5.1.5",
33+
"readable-stream": "^3.6.0"
3334
},
3435
"devDependencies": {
35-
"nyc": "^6.1.1",
36-
"standard": "^6.0.8",
37-
"tape": "^4.5.1"
36+
"nyc": "^15.0.1",
37+
"standard": "^14.3.3",
38+
"tape": "^5.0.0"
3839
},
3940
"browser": "browser/index.js"
4041
}

test/index.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var Buffer = require('buffer').Buffer
12
var asn1 = require('parse-asn1/asn1')
23
var test = require('tape').test
34
var nCrypto = require('crypto')
@@ -9,20 +10,20 @@ function isNode10 () {
910
}
1011

1112
fixtures.valid.rsa.forEach(function (f) {
12-
var message = new Buffer(f.message)
13-
var pub = new Buffer(f.public, 'base64')
13+
var message = Buffer.from(f.message)
14+
var pub = Buffer.from(f.public, 'base64')
1415
var priv
1516

1617
// skip passphrase tests in node 10
1718
if (f.passphrase && isNode10()) return
1819

1920
if (f.passphrase) {
2021
priv = {
21-
key: new Buffer(f.private, 'base64'),
22+
key: Buffer.from(f.private, 'base64'),
2223
passphrase: f.passphrase
2324
}
2425
} else {
25-
priv = new Buffer(f.private, 'base64')
26+
priv = Buffer.from(f.private, 'base64')
2627
}
2728

2829
test(f.message, function (t) {
@@ -46,23 +47,23 @@ fixtures.valid.rsa.forEach(function (f) {
4647
})
4748

4849
fixtures.valid.ec.forEach(function (f) {
49-
var message = new Buffer(f.message)
50-
var pub = new Buffer(f.public, 'base64')
50+
var message = Buffer.from(f.message)
51+
var pub = Buffer.from(f.public, 'base64')
5152
var priv
5253

5354
// skip passphrase tests in node 10
5455
if (f.passphrase && isNode10()) return
5556

5657
if (f.passphrase) {
5758
priv = {
58-
key: new Buffer(f.private, 'base64'),
59+
key: Buffer.from(f.private, 'base64'),
5960
passphrase: f.passphrase
6061
}
6162
} else {
62-
priv = new Buffer(f.private, 'base64')
63+
priv = Buffer.from(f.private, 'base64')
6364
}
6465

65-
test(f.message, function (t) {
66+
(nCrypto.getHashes().includes(f.scheme) ? test : test.skip)(f.message, function (t) {
6667
var nSign = nCrypto.createSign(f.scheme)
6768
var bSign = bCrypto.createSign(f.scheme)
6869

@@ -103,7 +104,7 @@ fixtures.valid.ec.forEach(function (f) {
103104

104105
fixtures.valid.kvectors.forEach(function (f) {
105106
test('kvector algo: ' + f.algo + ' key len: ' + f.key.length + ' msg: ' + f.msg, function (t) {
106-
var key = new Buffer(f.key, 'base64')
107+
var key = Buffer.from(f.key, 'base64')
107108

108109
var bSig = bCrypto.createSign(f.algo).update(f.msg).sign(key)
109110
var bRS = asn1.signature.decode(bSig, 'der')
@@ -116,9 +117,9 @@ fixtures.valid.kvectors.forEach(function (f) {
116117

117118
fixtures.invalid.verify.forEach(function (f) {
118119
test(f.description, function (t) {
119-
var sign = new Buffer(f.signature, 'hex')
120-
var pub = new Buffer(f.public, 'base64')
121-
var message = new Buffer(f.message)
120+
var sign = Buffer.from(f.signature, 'hex')
121+
var pub = Buffer.from(f.public, 'base64')
122+
var message = Buffer.from(f.message)
122123

123124
var nVerify = nCrypto.createVerify(f.scheme).update(message).verify(pub, sign)
124125
t.notOk(nVerify, 'node rejects it')

0 commit comments

Comments
 (0)