1
|
var crypto = require('crypto')
|
2
|
var tape = require('tape')
|
3
|
var Sha1 = require('../').sha1
|
4
|
|
5
|
var inputs = [
|
6
|
['', 'ascii'],
|
7
|
['abc', 'ascii'],
|
8
|
['123', 'ascii'],
|
9
|
['123456789abcdef123456789abcdef123456789abcdef123456789abcdef', 'ascii'],
|
10
|
['123456789abcdef123456789abcdef123456789abcdef123456789abc', 'ascii'],
|
11
|
['123456789abcdef123456789abcdef123456789abcdef123456789ab', 'ascii'],
|
12
|
['0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde', 'ascii'],
|
13
|
['0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef', 'ascii'],
|
14
|
['foobarbaz', 'ascii']
|
15
|
]
|
16
|
|
17
|
tape("hash is the same as node's crypto", function (t) {
|
18
|
inputs.forEach(function (v) {
|
19
|
var a = new Sha1().update(v[0], v[1]).digest('hex')
|
20
|
var e = crypto.createHash('sha1').update(v[0], v[1]).digest('hex')
|
21
|
console.log(a, '==', e)
|
22
|
t.equal(a, e)
|
23
|
})
|
24
|
|
25
|
t.end()
|
26
|
})
|
27
|
|
28
|
tape('call update multiple times', function (t) {
|
29
|
inputs.forEach(function (v) {
|
30
|
var hash = new Sha1()
|
31
|
var _hash = crypto.createHash('sha1')
|
32
|
|
33
|
for (var i = 0; i < v[0].length; i = (i + 1) * 2) {
|
34
|
var s = v[0].substring(i, (i + 1) * 2)
|
35
|
hash.update(s, v[1])
|
36
|
_hash.update(s, v[1])
|
37
|
}
|
38
|
|
39
|
var a = hash.digest('hex')
|
40
|
var e = _hash.digest('hex')
|
41
|
console.log(a, '==', e)
|
42
|
t.equal(a, e)
|
43
|
})
|
44
|
t.end()
|
45
|
})
|
46
|
|
47
|
tape('call update twice', function (t) {
|
48
|
var _hash = crypto.createHash('sha1')
|
49
|
var hash = new Sha1()
|
50
|
|
51
|
_hash.update('foo', 'ascii')
|
52
|
hash.update('foo', 'ascii')
|
53
|
|
54
|
_hash.update('bar', 'ascii')
|
55
|
hash.update('bar', 'ascii')
|
56
|
|
57
|
_hash.update('baz', 'ascii')
|
58
|
hash.update('baz', 'ascii')
|
59
|
|
60
|
var a = hash.digest('hex')
|
61
|
var e = _hash.digest('hex')
|
62
|
|
63
|
t.equal(a, e)
|
64
|
t.end()
|
65
|
})
|
66
|
|
67
|
tape('hex encoding', function (t) {
|
68
|
inputs.forEach(function (v) {
|
69
|
var hash = new Sha1()
|
70
|
var _hash = crypto.createHash('sha1')
|
71
|
|
72
|
for (var i = 0; i < v[0].length; i = (i + 1) * 2) {
|
73
|
var s = v[0].substring(i, (i + 1) * 2)
|
74
|
hash.update(Buffer.from(s, 'ascii').toString('hex'), 'hex')
|
75
|
_hash.update(Buffer.from(s, 'ascii').toString('hex'), 'hex')
|
76
|
}
|
77
|
var a = hash.digest('hex')
|
78
|
var e = _hash.digest('hex')
|
79
|
|
80
|
console.log(a, '==', e)
|
81
|
t.equal(a, e)
|
82
|
})
|
83
|
|
84
|
t.end()
|
85
|
})
|
86
|
|
87
|
tape('call digest for more than MAX_UINT32 bits of data', function (t) {
|
88
|
var _hash = crypto.createHash('sha1')
|
89
|
var hash = new Sha1()
|
90
|
var bigData = Buffer.alloc(0x1ffffffff / 8)
|
91
|
|
92
|
hash.update(bigData)
|
93
|
_hash.update(bigData)
|
94
|
|
95
|
var a = hash.digest('hex')
|
96
|
var e = _hash.digest('hex')
|
97
|
|
98
|
t.equal(a, e)
|
99
|
t.end()
|
100
|
})
|