-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.js
140 lines (137 loc) · 5.37 KB
/
hash.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const cronometro = require('cronometro')
const { createHash, randomBytes } = require('crypto')
const sodium = require('sodium-native')
const shortData = randomBytes(64)
const mediumData = randomBytes(384)
const longData = randomBytes(4096)
const megaData = randomBytes(2 ** 23)
const output256 = Buffer.alloc(32)
const output512 = Buffer.alloc(64)
const state = Buffer.alloc(sodium.crypto_generichash_STATEBYTES)
function size (name, data) {
return cronometro({
'crypto sha256 (no prealloc)': function () {
createHash('sha256').update(data).digest()
},
'crypto sha256 (prealloc)': function () {
createHash('sha256').update(data).digest(output256)
},
'crypto sha512 (no prealloc)': function () {
createHash('sha512').update(data).digest()
},
'crypto sha512 (prealloc)': function () {
createHash('sha512').update(data).digest(output512)
},
'crypto blake2b512 (no prealloc)': function () {
createHash('blake2b512').update(data).digest()
},
'crypto blake2b512 (prealloc)': function () {
createHash('blake2b512').update(data).digest(output512)
},
'sodium-native generichash (prealloc)': function () {
sodium.crypto_generichash(output512, data)
},
'sodium-native generichash (state reuse)': function () {
sodium.crypto_generichash_init(state, null, 64)
sodium.crypto_generichash_update(state, data)
sodium.crypto_generichash_final(state, output512)
},
'sodium-native generichash (32, prealloc)': function () {
sodium.crypto_generichash(output256, data)
},
'sodium-native generichash (32, state reuse)': function () {
sodium.crypto_generichash_init(state, null, 32)
sodium.crypto_generichash_update(state, data)
sodium.crypto_generichash_final(state, output256)
},
'sodium-native generichash (no prealloc)': function () {
const output512 = Buffer.alloc(64)
sodium.crypto_generichash(output512, data)
},
'sodium-native generichash (state no reuse)': function () {
const output512 = Buffer.alloc(64)
const state = Buffer.alloc(sodium.crypto_generichash_STATEBYTES)
sodium.crypto_generichash_init(state, null, 64)
sodium.crypto_generichash_update(state, data)
sodium.crypto_generichash_final(state, output512)
},
'sodium-native generichash (32, no prealloc)': function () {
const output256 = Buffer.alloc(32)
sodium.crypto_generichash(output256, data)
},
'sodium-native generichash (32, state no reuse)': function () {
const output256 = Buffer.alloc(32)
const state = Buffer.alloc(sodium.crypto_generichash_STATEBYTES)
sodium.crypto_generichash_init(state, null, 32)
sodium.crypto_generichash_update(state, data)
sodium.crypto_generichash_final(state, output256)
},
'crypto sha256 (no prealloc, digest)': function () {
createHash('sha256').update(data).digest('hex')
},
'crypto sha512 (no prealloc, digest)': function () {
createHash('sha512').update(data).digest('hex')
},
'crypto blake2b512 (no prealloc, digest)': function () {
createHash('blake2b512').update(data).digest('hex')
},
'sodium-native generichash (prealloc, digest)': function () {
sodium.crypto_generichash(output512, data)
output512.toString('hex')
},
'sodium-native generichash (state reuse, digest)': function () {
sodium.crypto_generichash_init(state, null, 64)
sodium.crypto_generichash_update(state, data)
sodium.crypto_generichash_final(state, output512)
output512.toString('hex')
},
'sodium-native generichash (32, prealloc, digest)': function () {
sodium.crypto_generichash(output256, data)
output256.toString('hex')
},
'sodium-native generichash (32, state reuse, digest)': function () {
sodium.crypto_generichash_init(state, null, 32)
sodium.crypto_generichash_update(state, data)
sodium.crypto_generichash_final(state, output256)
output256.toString('hex')
},
'sodium-native generichash (no prealloc, digest)': function () {
const output512 = Buffer.alloc(64)
sodium.crypto_generichash(output512, data)
output512.toString('hex')
},
'sodium-native generichash (state no reuse, digest)': function () {
const output512 = Buffer.alloc(64)
const state = Buffer.alloc(sodium.crypto_generichash_STATEBYTES)
sodium.crypto_generichash_init(state, null, 64)
sodium.crypto_generichash_update(state, data)
sodium.crypto_generichash_final(state, output512)
output512.toString('hex')
},
'sodium-native generichash (32, no prealloc, digest)': function () {
const output256 = Buffer.alloc(32)
sodium.crypto_generichash(output256, data)
output256.toString('hex')
},
'sodium-native generichash (32, state no reuse, digest)': function () {
const output256 = Buffer.alloc(32)
const state = Buffer.alloc(sodium.crypto_generichash_STATEBYTES)
sodium.crypto_generichash_init(state, null, 32)
sodium.crypto_generichash_update(state, data)
sodium.crypto_generichash_final(state, output256)
output256.toString('hex')
}
}, {
iterations: 10000,
print: {
compare: true,
compareMode: 'base'
}
})
}
;(async () => {
await size('64 bytes', shortData)
await size('384 bytes', mediumData)
await size('4096 bytes', longData)
await size('4 MB', megaData)
})()