forked from multiformats/js-multihashing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsha3.js
More file actions
52 lines (44 loc) · 923 Bytes
/
sha3.js
File metadata and controls
52 lines (44 loc) · 923 Bytes
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
'use strict'
const sha3 = require('js-sha3')
const functions = [
[0x14, sha3.sha3_512],
[0x15, sha3.sha3_384],
[0x16, sha3.sha3_256],
[0x17, sha3.sha3_224],
[0x18, sha3.shake128, 256],
[0x19, sha3.shake256, 512],
[0x1A, sha3.keccak224],
[0x1B, sha3.keccak256],
[0x1C, sha3.keccak384],
[0x1D, sha3.keccak512]
]
class Hasher {
constructor (hashFunc, arg) {
this.hf = hashFunc
this.arg = arg
this.input = null
}
update (buf) {
this.input = buf
return this
}
digest () {
const input = this.input
const arg = this.arg
return Buffer.from(this.hf(input, arg), 'hex')
}
}
function addFuncs (table) {
for (const info of functions) {
const code = info[0]
const fn = info[1]
if (info.length === 3) {
table[code] = () => new Hasher(fn, info[2])
} else {
table[code] = () => new Hasher(fn)
}
}
}
module.exports = {
addFuncs
}