Skip to content

Commit f9ee3c0

Browse files
committed
test with published test vectors
1 parent 10c18d9 commit f9ee3c0

File tree

3 files changed

+57
-42
lines changed

3 files changed

+57
-42
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"sha.js": "2.1.3"
2020
},
2121
"devDependencies": {
22-
"tape": "~2.3.2"
22+
"tape": "~2.3.2",
23+
"hash-test-vectors": "~1.1.0"
2324
},
2425
"testling": {
2526
"files": "test/{create-hash,create-hmac,simple}.js",

test/create-hash.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,32 @@ var assertSame = require('./util').same
22
var fs = require('fs')
33
var test = require('tape')
44

5-
var algorithms = ['sha1', 'sha256', 'md5', 'rmd160'];
5+
var algorithms = require('../').getHashes()
66
var encodings = [/*'binary',*/ 'hex', 'base64'];
7+
var vectors = require('hash-test-vectors')
78

9+
var createHash = require('../create-hash')
810

911
algorithms.forEach(function (algorithm) {
10-
encodings.forEach(function (encoding) {
11-
assertSame(algorithm + ' hash using ' + encoding, function (crypto, cb) {
12-
cb(null, crypto.createHash(algorithm).update('hellø', 'utf-8').digest(encoding));
13-
})
12+
test('test ' + algorithm + ' against test vectors', function (t) {
1413

15-
assertSame(algorithm + ' hmac using ' + encoding, function (crypto, cb) {
16-
cb(null, crypto.createHmac(algorithm, 'secret').update('hellø', 'utf-8').digest(encoding))
17-
})
18-
});
14+
vectors.forEach(function (obj, i) {
15+
var input = new Buffer(obj.input, 'base64')
16+
var node = obj[algorithm]
17+
var js = createHash(algorithm).update(input).digest('hex')
18+
t.equal(js, node, algorithm + '(testVector['+i+']) == ' + node)
19+
})
1920

20-
assertSame(algorithm + ' with raw binary', function (crypto, cb) {
21-
var seed = 'hellø';
22-
for (var i = 0; i < 1000; i++) {
23-
seed = crypto.createHash(algorithm).update(new Buffer(seed)).digest('binary');
24-
}
25-
cb(null, crypto.createHash(algorithm).update(new Buffer(seed)).digest('hex'));
21+
encodings.forEach(function (encoding) {
22+
vectors.forEach(function (obj, i) {
23+
var input = new Buffer(obj.input, 'base64').toString(encoding)
24+
var node = obj[algorithm]
25+
var js = createHash(algorithm).update(input, encoding).digest('hex')
26+
t.equal(js, node, algorithm + '(testVector['+i+'], '+encoding+') == ' + node)
27+
})
2628
});
2729

28-
assertSame(algorithm + ' empty string', function (crypto, cb) {
29-
cb(null, crypto.createHash(algorithm).update('').digest('hex'));
30-
});
30+
t.end()
31+
})
3132
});
3233

test/create-hmac.js

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,45 @@ var same = require('./util').same
33
var join = require('./util').join
44
var test = require('tape')
55

6-
var data = require('crypto').pseudoRandomBytes(256)
7-
8-
;['sha1', 'sha256', 'md5'].forEach(function (alg) {
9-
console.log('alg', alg)
10-
;['p455w0rd', 'secretz', 'whatevs', 'such secure, wow', ''].forEach(function (pass) {
11-
same('createHmac('+alg+', ' + JSON.stringify(pass) + ')' , function (crypto, cb) {
12-
var r = crypto
13-
.createHmac(alg, new Buffer(pass, 'ascii'))
14-
.digest('hex')
15-
console.log(r)
16-
cb(null, r)
6+
var algorithms = require('../').getHashes()
7+
var vectors = require('hash-test-vectors/hmac')
8+
var createHmac = require('../create-hmac')
9+
10+
algorithms.forEach(function (alg) {
11+
12+
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])
1719
})
20+
t.end()
1821
})
1922

20-
for(var i = 8; i < data.length; i += 7)
21-
(function (i) {
22-
var pass = data.slice(0, i)
23-
same('createHmac('+alg+', pseudoRandomBytes('+ i + ')' , function (crypto, cb) {
24-
var r = crypto
25-
.createHmac(alg, pass)
26-
.digest('hex')
27-
console.log(r)
28-
cb(null, r)
29-
})
30-
}(i))
31-
23+
// ;['p455w0rd', 'secretz', 'whatevs', 'such secure, wow', ''].forEach(function (pass) {
24+
// same('createHmac('+alg+', ' + JSON.stringify(pass) + ')' , function (crypto, cb) {
25+
// var r = crypto
26+
// .createHmac(alg, new Buffer(pass, 'ascii'))
27+
// .digest('hex')
28+
// console.log(r)
29+
// cb(null, r)
30+
// })
31+
// })
32+
//
33+
// for(var i = 8; i < data.length; i += 7)
34+
// (function (i) {
35+
// var pass = data.slice(0, i)
36+
// same('createHmac('+alg+', pseudoRandomBytes('+ i + ')' , function (crypto, cb) {
37+
// var r = crypto
38+
// .createHmac(alg, pass)
39+
// .digest('hex')
40+
// console.log(r)
41+
// cb(null, r)
42+
// })
43+
// }(i))
44+
//
3245

3346
})
3447

0 commit comments

Comments
 (0)