Skip to content

Commit 26646d1

Browse files
committed
test both imported version and one with this library
1 parent 4455ed4 commit 26646d1

File tree

3 files changed

+61
-56
lines changed

3 files changed

+61
-56
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
"browserify-aes": "0.8.1",
2020
"browserify-sign": "2.8.0",
2121
"create-ecdh": "1.0.3",
22-
"create-hash": "^1.0.2",
23-
"create-hmac": "^1.0.2",
22+
"create-hash": "^1.1.0",
23+
"create-hmac": "^1.1.0",
2424
"diffie-hellman": "2.2.3",
2525
"inherits": "^2.0.1",
2626
"pbkdf2-compat": "2.0.1",

test/create-hash.js

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,38 @@ var test = require('tape')
44
var algorithms = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160']
55
var encodings = [/*'binary',*/ 'hex', 'base64'];
66
var vectors = require('hash-test-vectors')
7+
testLib('createHash in crypto-browserify',require('../').createHash);
8+
testLib('create-hash/browser',require('create-hash/browser'));
9+
function testLib(name, createHash) {
10+
test(name, function (t){
11+
algorithms.forEach(function (algorithm) {
12+
t.test('test ' + algorithm + ' against test vectors', function (t) {
13+
vectors.forEach(function (obj, i) {
14+
var input = new Buffer(obj.input, 'base64')
15+
var node = obj[algorithm]
16+
var js = createHash(algorithm).update(input).digest('hex')
17+
t.equal(js, node, algorithm + '(testVector['+i+']) == ' + node)
18+
})
719

8-
var createHash = require('../').createHash
9-
10-
algorithms.forEach(function (algorithm) {
11-
test('test ' + algorithm + ' against test vectors', function (t) {
12-
vectors.forEach(function (obj, i) {
13-
var input = new Buffer(obj.input, 'base64')
14-
var node = obj[algorithm]
15-
var js = createHash(algorithm).update(input).digest('hex')
16-
t.equal(js, node, algorithm + '(testVector['+i+']) == ' + node)
17-
})
18-
19-
encodings.forEach(function (encoding) {
20+
encodings.forEach(function (encoding) {
21+
vectors.forEach(function (obj, i) {
22+
var input = new Buffer(obj.input, 'base64').toString(encoding)
23+
var node = obj[algorithm]
24+
var js = createHash(algorithm).update(input, encoding).digest('hex')
25+
t.equal(js, node, algorithm + '(testVector['+i+'], '+encoding+') == ' + node)
26+
})
27+
});
2028
vectors.forEach(function (obj, i) {
21-
var input = new Buffer(obj.input, 'base64').toString(encoding)
29+
var input = new Buffer(obj.input, 'base64')
2230
var node = obj[algorithm]
23-
var js = createHash(algorithm).update(input, encoding).digest('hex')
24-
t.equal(js, node, algorithm + '(testVector['+i+'], '+encoding+') == ' + node)
31+
var hash = createHash(algorithm);
32+
hash.end(input)
33+
var js = hash.read().toString('hex')
34+
t.equal(js, node, algorithm + '(testVector['+i+']) == ' + node)
2535
})
36+
t.end()
37+
})
2638
});
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-
})
35-
t.end()
36-
})
37-
});
3839

40+
});
41+
}

test/create-hmac.js

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,37 @@ var test = require('tape')
33

44
var algorithms = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160']
55
var vectors = require('hash-test-vectors/hmac')
6-
var createHmac = require('../').createHmac
6+
testLib('createHmac in crypto-browserify',require('../').createHmac);
7+
testLib('create-hmac/browser',require('create-hmac/browser'));
8+
function testLib(name, createHmac) {
9+
test(name, function (t){
10+
algorithms.forEach(function (alg) {
11+
12+
t.test('hmac('+alg+')', function (t) {
13+
vectors.forEach(function (input, i) {
14+
var output = createHmac(alg, new Buffer(input.key, 'hex'))
15+
.update(input.data, 'hex').digest()
16+
17+
output = input.truncate ? output.slice(0, input.truncate) : output
18+
t.equal(output.toString('hex'), input[alg])
19+
})
20+
t.end()
21+
})
22+
23+
t.test('hmac('+alg+')', function (t) {
24+
vectors.forEach(function (input, i) {
25+
var hmac = createHmac(alg, new Buffer(input.key, 'hex'))
26+
27+
hmac.end(input.data, 'hex')
28+
var output = hmac.read()
29+
30+
output = input.truncate ? output.slice(0, input.truncate) : output
31+
t.equal(output.toString('hex'), input[alg])
32+
})
33+
t.end()
34+
})
735

8-
algorithms.forEach(function (alg) {
9-
10-
test('hmac('+alg+')', function (t) {
11-
vectors.forEach(function (input, i) {
12-
var output = createHmac(alg, new Buffer(input.key, 'hex'))
13-
.update(input.data, 'hex').digest()
14-
15-
output = input.truncate ? output.slice(0, input.truncate) : output
16-
t.equal(output.toString('hex'), input[alg])
17-
})
18-
t.end()
19-
})
20-
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])
3036
})
31-
t.end()
3237
})
3338

34-
})
35-
36-
37-
39+
}

0 commit comments

Comments
 (0)