UNPKG

1.5 kBJavaScriptView Raw
1var test = require('tape')
2
3var algorithms = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160']
4var vectors = require('hash-test-vectors/hmac')
5testLib('createHmac in crypto-browserify', require('../').createHmac)
6testLib('create-hmac/browser', require('create-hmac/browser'))
7
8function testLib (name, createHmac) {
9 algorithms.forEach(function (alg) {
10 test(name + ' hmac(' + alg + ')', function (t) {
11 run(0)
12 function run (i) {
13 if (i >= vectors.length) {
14 return t.end()
15 }
16 var input = vectors[i]
17 var output = createHmac(alg, new Buffer(input.key, 'hex'))
18 .update(input.data, 'hex').digest()
19
20 output = input.truncate ? output.slice(0, input.truncate) : output
21 output = output.toString('hex')
22 if (output !== input[alg]) {
23 t.equal(output, input[alg])
24 }
25 setTimeout(run, 0, i + 1)
26 }
27 })
28
29 test('hmac(' + alg + ')', function (t) {
30 run(0)
31 function run (i) {
32 if (i >= vectors.length) {
33 return t.end()
34 }
35 var input = vectors[i]
36 var hmac = createHmac(alg, new Buffer(input.key, 'hex'))
37
38 hmac.end(input.data, 'hex')
39 var output = hmac.read()
40
41 output = input.truncate ? output.slice(0, input.truncate) : output
42 output = output.toString('hex')
43 if (output !== input[alg]) {
44 t.equal(output, input[alg])
45 }
46 setTimeout(run, 0, i + 1)
47 }
48 })
49 })
50}