Skip to content

Commit bedb9f0

Browse files
committed
merge
2 parents 3b6d236 + a4ed533 commit bedb9f0

File tree

10 files changed

+263
-43
lines changed

10 files changed

+263
-43
lines changed

create-hash.js

Lines changed: 76 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,82 @@
11
var createHash = require('sha.js')
22

3-
var md5 = toConstructor(require('./md5'))
4-
var rmd160 = toConstructor(require('ripemd160'))
5-
6-
function toConstructor (fn) {
7-
return function () {
8-
var buffers = []
9-
var m= {
10-
update: function (data, enc) {
11-
if(!Buffer.isBuffer(data)) data = new Buffer(data, enc)
12-
buffers.push(data)
13-
return this
14-
},
15-
digest: function (enc) {
16-
var buf = Buffer.concat(buffers)
17-
var r = fn(buf)
18-
buffers = null
19-
return enc ? r.toString(enc) : r
20-
}
21-
}
22-
return m
3+
var md5 = require('./md5')
4+
var rmd160 = require('ripemd160')
5+
var Transform = require('stream').Transform;
6+
var inherits = require('util').inherits
7+
8+
module.exports = function (alg) {
9+
if('md5' === alg) return new HashNoConstructor(md5)
10+
if('rmd160' === alg) return new HashNoConstructor(rmd160)
11+
return new Hash(createHash(alg))
12+
}
13+
inherits(HashNoConstructor, Transform)
14+
15+
function HashNoConstructor(hash) {
16+
Transform.call(this);
17+
this._hash = hash
18+
this.buffers = []
19+
}
20+
21+
HashNoConstructor.prototype._transform = function (data, _, done) {
22+
this.buffers.push(data)
23+
done()
24+
}
25+
HashNoConstructor.prototype._flush = function (done) {
26+
var buf = Buffer.concat(this.buffers)
27+
var r = this._hash(buf)
28+
this.buffers = null
29+
this.push(r)
30+
done()
31+
}
32+
HashNoConstructor.prototype.update = function (data, enc) {
33+
this.write(data, enc)
34+
return this
35+
}
36+
37+
HashNoConstructor.prototype.digest = function (enc) {
38+
this.end()
39+
var outData = new Buffer('')
40+
var chunk
41+
while ((chunk = this.read())) {
42+
outData = Buffer.concat([outData, chunk])
43+
}
44+
if (enc) {
45+
outData = outData.toString(enc)
2346
}
47+
return outData
2448
}
2549

26-
module.exports = function (alg) {
27-
if('md5' === alg) return new md5()
28-
if('rmd160' === alg) return new rmd160()
29-
return createHash(alg)
50+
inherits(Hash, Transform)
51+
52+
function Hash(hash) {
53+
Transform.call(this);
54+
this._hash = hash
55+
}
56+
57+
Hash.prototype._transform = function (data, _, done) {
58+
this._hash.update(data)
59+
done()
60+
}
61+
Hash.prototype._flush = function (done) {
62+
this.push(this._hash.digest())
63+
this._hash = null
64+
done()
65+
}
66+
Hash.prototype.update = function (data, enc) {
67+
this.write(data, enc)
68+
return this
69+
}
70+
71+
Hash.prototype.digest = function (enc) {
72+
this.end()
73+
var outData = new Buffer('')
74+
var chunk
75+
while ((chunk = this.read())) {
76+
outData = Buffer.concat([outData, chunk])
77+
}
78+
if (enc) {
79+
outData = outData.toString(enc)
80+
}
81+
return outData
3082
}

create-hmac.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
var createHash = require('./create-hash')
2-
2+
var Transform = require('stream').Transform;
3+
var inherits = require('util').inherits
34
var zeroBuffer = new Buffer(128)
45
zeroBuffer.fill(0)
56

67
module.exports = Hmac
7-
8+
inherits(Hmac, Transform)
89
function Hmac (alg, key) {
910
if(!(this instanceof Hmac)) return new Hmac(alg, key)
11+
12+
Transform.call(this)
1013
this._opad = opad
1114
this._alg = alg
1215

@@ -32,12 +35,31 @@ function Hmac (alg, key) {
3235
}
3336

3437
Hmac.prototype.update = function (data, enc) {
35-
this._hash.update(data, enc)
38+
this.write(data, enc)
3639
return this
3740
}
3841

39-
Hmac.prototype.digest = function (enc) {
42+
Hmac.prototype._transform = function (data, _, next) {
43+
this._hash.update(data)
44+
next()
45+
}
46+
47+
Hmac.prototype._flush = function (next) {
4048
var h = this._hash.digest()
41-
return createHash(this._alg).update(this._opad).update(h).digest(enc)
49+
this.push(createHash(this._alg).update(this._opad).update(h).digest())
50+
next()
51+
}
52+
53+
Hmac.prototype.digest = function (enc) {
54+
this.end()
55+
var outData = new Buffer('')
56+
var chunk
57+
while ((chunk = this.read())) {
58+
outData = Buffer.concat([outData, chunk])
59+
}
60+
if (enc) {
61+
outData = outData.toString(enc)
62+
}
63+
return outData
4264
}
4365

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ var p = require('./pbkdf2')(exports)
3636
exports.pbkdf2 = p.pbkdf2
3737
exports.pbkdf2Sync = p.pbkdf2Sync
3838
require('browserify-aes/inject')(exports, module.exports);
39+
require('browserify-sign/inject')(module.exports, exports);
40+
require('diffie-hellman/inject')(exports, module.exports);
3941

4042
// the least I can do is make error messages for the rest of the node.js/crypto api.
41-
each(['createCredentials'
42-
, 'createSign'
43-
, 'createVerify'
44-
, 'createDiffieHellman'
43+
each([
44+
'createCredentials',
4545
], function (name) {
4646
exports[name] = function () {
4747
error('sorry,', name, 'is not implemented yet')

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,25 @@
22
"author": "Dominic Tarr <[email protected]> (dominictarr.com)",
33
"name": "crypto-browserify",
44
"description": "partial implementation of crypto for the browser",
5-
"version": "3.3.0",
5+
"version": "3.4.0",
66
"homepage": "https://github.com/dominictarr/crypto-browserify",
77
"repository": {
88
"type": "git",
99
"url": "git://github.com/dominictarr/crypto-browserify.git"
1010
},
1111
"scripts": {
12-
"test": "set -e; for t in test/*.js; do node $t; done"
12+
"test": "set -e; for t in test/node/*.js test/*.js; do node $t; done"
1313
},
1414
"engines": {
1515
"node": "*"
1616
},
1717
"dependencies": {
18+
"browserify-aes": "0.6.0",
19+
"browserify-sign": "2.4.0",
1820
"pbkdf2-compat": "2.0.1",
1921
"ripemd160": "0.2.0",
2022
"sha.js": "2.2.6",
21-
"browserify-aes": "0.6.0"
23+
"diffie-hellman": "2.2.0"
2224
},
2325
"devDependencies": {
2426
"tape": "~2.3.2",

readme.markdown

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@ Here is the subset that is currently implemented:
1818
* randomBytes
1919
* createCipher (aes)
2020
* createDecipher (aes)
21+
* createDiffieHellman
2122

22-
## TODO
23+
## todo
2324

24-
The highest priority unimplemented features are
25+
these features from node's `crypto` are still unimplemented.
2526

26-
* createDiffieHelman
27-
* createSign (rsa)
28-
* createVerify (rsa)
27+
* createCredentials
2928

3029
## contributions
3130

test/create-hash.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ var createHash = require('../create-hash')
99

1010
algorithms.forEach(function (algorithm) {
1111
test('test ' + algorithm + ' against test vectors', function (t) {
12-
1312
vectors.forEach(function (obj, i) {
1413
var input = new Buffer(obj.input, 'base64')
1514
var node = obj[algorithm]
@@ -25,7 +24,14 @@ algorithms.forEach(function (algorithm) {
2524
t.equal(js, node, algorithm + '(testVector['+i+'], '+encoding+') == ' + node)
2625
})
2726
});
28-
27+
vectors.forEach(function (obj, i) {
28+
var input = new Buffer(obj.input, 'base64')
29+
var node = obj[algorithm]
30+
var hash = createHash(algorithm);
31+
hash.end(input)
32+
var js = hash.read().toString('hex')
33+
t.equal(js, node, algorithm + '(testVector['+i+']) == ' + node)
34+
})
2935
t.end()
3036
})
3137
});

test/create-hmac.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ algorithms.forEach(function (alg) {
1818
t.end()
1919
})
2020

21+
test('hmac('+alg+')', function (t) {
22+
vectors.forEach(function (input, i) {
23+
var hmac = createHmac(alg, new Buffer(input.key, 'hex'))
24+
25+
hmac.end(input.data, 'hex')
26+
var output = hmac.read()
27+
28+
output = input.truncate ? output.slice(0, input.truncate) : output
29+
t.equal(output.toString('hex'), input[alg])
30+
})
31+
t.end()
32+
})
33+
2134
})
2235

2336

test/dh.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
var test = require('tape');
2+
var crypto = require('../');
3+
test('diffie-hellman mod groups', function (t) {
4+
[
5+
'modp1', 'modp2', 'modp5', 'modp14', 'modp15', 'modp16'
6+
].forEach(function (mod) {
7+
t.test(mod, function (t){
8+
t.plan(3);
9+
var dh1 = crypto.getDiffieHellman(mod);
10+
var p1 = dh1.getPrime().toString('hex');
11+
dh1.generateKeys();
12+
var dh2 = crypto.getDiffieHellman(mod);
13+
var p2 = dh2.getPrime().toString('hex');
14+
dh2.generateKeys();
15+
t.equals(p1, p2, 'equal primes');
16+
var pubk1 = dh1.getPublicKey();
17+
var pubk2 = dh2.getPublicKey();
18+
t.notEquals(pubk1, pubk2, 'diff public keys');
19+
var pub1 = dh1.computeSecret(pubk2).toString('hex');
20+
var pub2 = dh2.computeSecret(dh1.getPublicKey()).toString('hex');
21+
t.equals(pub1, pub2, 'equal secrets');
22+
});
23+
});
24+
});
25+
test('diffie-hellman key lengths', function (t) {
26+
[
27+
64, 65, 192
28+
].forEach(function (len) {
29+
t.test('' + len, function (t){
30+
t.plan(3);
31+
var dh2 = crypto.createDiffieHellman(len);
32+
var prime2 = dh2.getPrime();
33+
var p2 = prime2.toString('hex');
34+
var dh1 = crypto.createDiffieHellman(prime2);
35+
var p1 = dh1.getPrime().toString('hex');
36+
dh1.generateKeys();
37+
dh2.generateKeys();
38+
t.equals(p1, p2, 'equal primes');
39+
var pubk1 = dh1.getPublicKey();
40+
var pubk2 = dh2.getPublicKey();
41+
t.notEquals(pubk1, pubk2, 'diff public keys');
42+
var pub1 = dh1.computeSecret(pubk2).toString('hex');
43+
var pub2 = dh2.computeSecret(dh1.getPublicKey()).toString('hex');
44+
t.equals(pub1, pub2, 'equal secrets');
45+
});
46+
});
47+
});

test/node/dh.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
var test = require('tape');
2+
var cryptoB = require('../../');
3+
var crypto = require('crypto')
4+
5+
test('diffie-hellman mod groups', function (t) {
6+
[
7+
'modp1', 'modp2', 'modp5', 'modp14', 'modp15', 'modp16'
8+
].forEach(function (mod) {
9+
t.test(mod, function (t){
10+
t.plan(3);
11+
var dh1 = cryptoB.getDiffieHellman(mod);
12+
var p1 = dh1.getPrime().toString('hex');
13+
dh1.generateKeys();
14+
15+
var dh2 = crypto.getDiffieHellman(mod);
16+
var p2 = dh2.getPrime().toString('hex');
17+
dh2.generateKeys();
18+
t.equals(p1, p2, 'equal primes');
19+
var pubk1 = dh1.getPublicKey();
20+
var pubk2 = dh2.getPublicKey();
21+
t.notEquals(pubk1, pubk2, 'diff public keys');
22+
var pub1 = dh1.computeSecret(pubk2).toString('hex');
23+
var pub2 = dh2.computeSecret(pubk1).toString('hex');
24+
t.equals(pub1, pub2, 'equal secrets');
25+
});
26+
});
27+
});
28+
29+
test('diffie-hellman key lengths', function (t) {
30+
[
31+
64, 65, 192
32+
].forEach(function (len) {
33+
t.test('' + len, function (t){
34+
t.plan(3);
35+
var dh2 = cryptoB.createDiffieHellman(len);
36+
var prime2 = dh2.getPrime();
37+
var p2 = prime2.toString('hex');
38+
var dh1 = crypto.createDiffieHellman(prime2);
39+
var p1 = dh1.getPrime().toString('hex');
40+
dh1.generateKeys();
41+
dh2.generateKeys();
42+
t.equals(p1, p2, 'equal primes');
43+
var pubk1 = dh1.getPublicKey();
44+
var pubk2 = dh2.getPublicKey();
45+
t.notEquals(pubk1, pubk2, 'diff public keys');
46+
var pub1 = dh1.computeSecret(pubk2).toString('hex');
47+
var pub2 = dh2.computeSecret(dh1.getPublicKey()).toString('hex');
48+
t.equals(pub1, pub2, 'equal secrets');
49+
});
50+
});
51+
});

test/sign.js

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)