Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80559 views
1
var hexpp = require('../hexpp').defaults({bigendian: false})
2
var tape = require('tape')
3
var Hash = require('../hash')
4
5
var hex = '0A1B2C3D4E5F6G7H', hexbuf
6
function equal(t, a, b) {
7
t.equal(a.length, b.length)
8
for(var i = 0; i < a.length; i++)
9
t.equal(a[i], b[i])
10
}
11
12
var count16 = {
13
strings: ['0A1B2C3D4E5F6G7H'],
14
buffers: [
15
hexbuf = new Buffer([
16
48, 65, 49, 66, 50, 67, 51, 68,
17
52, 69, 53, 70, 54, 71, 55, 72
18
]),
19
new Buffer([
20
128, 0, 0, 0, 0, 0, 0, 0,
21
0, 0, 0, 0, 0, 0, 0, 128
22
])
23
]
24
}
25
var empty = {
26
strings: [''],
27
buffers: [
28
new Buffer([
29
128, 0, 0, 0, 0, 0, 0, 0,
30
0, 0, 0, 0, 0, 0, 0, 0
31
])
32
]
33
}
34
var hh = 'abcdefhijklmnopq'
35
36
var multi = {
37
strings: ['abcd', 'efhijk', 'lmnopq'],
38
buffers: [
39
new Buffer('abcdefhijklmnopq', 'ascii'),
40
new Buffer([
41
128, 0, 0, 0, 0, 0, 0, 0,
42
0, 0, 0, 0, 0, 0, 0, 128
43
])
44
]
45
}
46
47
var long = {
48
strings: [hex+hex],
49
buffers: [
50
hexbuf,
51
hexbuf,
52
new Buffer([
53
128, 0, 0, 0, 0, 0, 0, 0,
54
0, 0, 0, 0, 0, 0, 1, 0
55
])
56
]
57
}
58
59
function makeTest(name, data) {
60
tape(name, function (t) {
61
62
var h = new Hash(16, 8)
63
var hash = new Buffer(20)
64
var n = 2
65
var expected = data.buffers.slice()
66
//t.plan(expected.length + 1)
67
h._update = function (block) {
68
var e = expected.shift()
69
console.log('---block---')
70
console.log(hexpp(block), block.length)
71
console.log('---e---')
72
console.log(hexpp(e), block.length)
73
console.log(block)
74
equal(t, block, e)
75
if(n < 0)
76
throw new Error('expecting only 2 calls to _update')
77
78
return hash
79
}
80
81
data.strings.forEach(function (string) {
82
h.update(string, 'ascii')
83
})
84
85
equal(t, h.digest(), hash)
86
t.end()
87
88
})
89
}
90
91
makeTest('Hash#update 1 in 1', count16)
92
makeTest('empty Hash#update', empty)
93
makeTest('Hash#update 1 in 3', multi)
94
makeTest('Hash#update 2 in 1', long)
95
96
97