-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.js
101 lines (82 loc) · 2.58 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var tape = require('tape')
var blake2b = require('./')
var vectors = require('blake2b/test-vectors.json')
var readyCalled = false
process.on('exit', function () {
if (!readyCalled)
throw new Error('ready not called')
})
blake2b.ready(function () {
readyCalled = true
tape('hello world', function (t) {
var hash = blake2b()
.update(Buffer.from('hello'))
.update(Buffer.from(' '))
.update(Buffer.from('world'))
.digest('hex')
t.same(hash, '256c83b297114d201b30179f3f0ef0cace9783622da5974326b436178aeef610')
t.end()
})
tape('hello world', function (t) {
var hash = blake2b(64)
.update(Buffer.from('hello'))
.update(Buffer.from(' '))
.update(Buffer.from('world'))
.digest('hex')
t.same(hash, '021ced8799296ceca557832ab941a50b4a11f83478cf141f51f933f653ab9fbcc05a037cddbed06e309bf334942c4e58cdf1a46e237911ccd7fcf9787cbc7fd0')
t.end()
})
tape('both at the same time', function (t) {
var a = blake2b()
var b = blake2b(64)
var hash = a
.update(Buffer.from('hello'))
.update(Buffer.from(' '))
.update(Buffer.from('world'))
.digest('hex')
t.same(hash, '256c83b297114d201b30179f3f0ef0cace9783622da5974326b436178aeef610')
var hash = b
.update(Buffer.from('hello'))
.update(Buffer.from(' '))
.update(Buffer.from('world'))
.digest('hex')
t.same(hash, '021ced8799296ceca557832ab941a50b4a11f83478cf141f51f933f653ab9fbcc05a037cddbed06e309bf334942c4e58cdf1a46e237911ccd7fcf9787cbc7fd0')
t.end()
})
tape('allows getting & setting a partial hash', function (t) {
var a = blake2b(64)
var b = blake2b(64)
var partialHash = a
.update(Buffer.from('hello'))
.update(Buffer.from(' '))
.update(Buffer.from('world'))
.getPartialHash()
b.setPartialHash(partialHash)
t.same(a.digest(), b.digest())
t.end()
})
vectors.forEach(function (vector, i) {
tape('test-vectors.json #' + i, function (t) {
var key = vector.key && Buffer.from(vector.key, 'hex')
var salt = vector.salt && Buffer.from(vector.salt, 'hex')
var personal = vector.personal && Buffer.from(vector.personal, 'hex')
var hash = blake2b(vector.outlen, key, salt, personal, true)
.update(Buffer.from(vector.input, 'hex'))
.digest('hex')
t.same(hash, vector.out)
t.end()
})
})
})
tape('.ready()', function (t) {
var invokeCount = 0;
blake2b.ready()
.then(function () {
invokeCount++
throw new Error()
})
.catch(function (err) {
t.same(invokeCount, 1)
t.end()
})
});