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