Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80540 views
1
2
var vectors = require('hash-test-vectors')
3
var tape = require('tape')
4
//var from = require('bops/typedarray/from')
5
var Buffer = require('buffer').Buffer
6
var hexpp = require('../hexpp')
7
8
var createHash = require('../')
9
10
function makeTest(alg, i, verbose) {
11
var v = vectors[i]
12
13
tape(alg + ': NIST vector ' + i, function (t) {
14
if(verbose) {
15
console.log(v)
16
console.log('VECTOR', i)
17
console.log('INPUT', v.input)
18
console.log(hexpp(new Buffer(v.input, 'base64')))
19
console.log(new Buffer(v.input, 'base64').toString('hex'))
20
}
21
var buf = new Buffer(v.input, 'base64')
22
t.equal(createHash(alg).update(buf).digest('hex'), v[alg])
23
24
i = ~~(buf.length / 2)
25
var buf1 = buf.slice(0, i)
26
var buf2 = buf.slice(i, buf.length)
27
28
console.log(buf1.length, buf2.length, buf.length)
29
console.log(createHash(alg)._block.length)
30
31
t.equal(
32
createHash(alg)
33
.update(buf1)
34
.update(buf2)
35
.digest('hex'),
36
v[alg]
37
)
38
39
var j, buf3
40
41
i = ~~(buf.length / 3)
42
j = ~~(buf.length * 2 / 3)
43
buf1 = buf.slice(0, i)
44
buf2 = buf.slice(i, j)
45
buf3 = buf.slice(j, buf.length)
46
47
t.equal(
48
createHash(alg)
49
.update(buf1)
50
.update(buf2)
51
.update(buf3)
52
.digest('hex'),
53
v[alg]
54
)
55
56
setTimeout(function () {
57
//avoid "too much recursion" errors in tape in firefox
58
t.end()
59
})
60
})
61
62
}
63
64
if(process.argv[2])
65
makeTest(process.argv[2], parseInt(process.argv[3]), true)
66
else
67
vectors.forEach(function (v, i) {
68
makeTest('sha1', i)
69
makeTest('sha224', i)
70
makeTest('sha256', i)
71
makeTest('sha384', i)
72
makeTest('sha512', i)
73
})
74
75
76
77
78