This repository has been archived by the owner on Aug 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.js
120 lines (107 loc) · 3 KB
/
index.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
'use strict'
const errcode = require('err-code')
const multihash = require('multihashes')
const crypto = require('./crypto')
const { equals } = require('uint8arrays/equals')
/**
* @typedef {import("./types").Digest} Digest
* @typedef {import("multihashes").HashName} HashName
*/
/**
* Hash the given `bytes` using the algorithm specified by `alg`.
*
* @param {Uint8Array} bytes - The value to hash.
* @param {HashName} alg - The algorithm to use eg 'sha1'
* @param {number} [length] - Optionally trim the result to this length.
* @returns {Promise<Uint8Array>}
*/
async function Multihashing (bytes, alg, length) {
const digest = await Multihashing.digest(bytes, alg, length)
return multihash.encode(digest, alg, length)
}
/**
* Expose multihash itself, to avoid silly double requires.
*/
Multihashing.multihash = multihash
/**
* @param {Uint8Array} bytes - The value to hash.
* @param {HashName} alg - The algorithm to use eg 'sha1'
* @param {number} [length] - Optionally trim the result to this length.
* @returns {Promise<Uint8Array>}
*/
Multihashing.digest = async (bytes, alg, length) => {
const hash = Multihashing.createHash(alg)
const digest = await hash(bytes)
return length ? digest.slice(0, length) : digest
}
/**
* Creates a function that hashes with the given algorithm
*
* @param {HashName} alg - The algorithm to use eg 'sha1'
* @returns {Digest} - The hash function corresponding to `alg`
*/
Multihashing.createHash = function (alg) {
if (!alg) {
const e = errcode(new Error('hash algorithm must be specified'), 'ERR_HASH_ALGORITHM_NOT_SPECIFIED')
throw e
}
const code = multihash.coerceCode(alg)
if (!Multihashing.functions[code]) {
throw errcode(new Error(`multihash function '${alg}' not yet supported`), 'ERR_HASH_ALGORITHM_NOT_SUPPORTED')
}
return Multihashing.functions[code]
}
/**
* Mapping of multihash codes to their hashing functions.
*
* @type {Record<number, Digest>}
*/
// @ts-ignore - most of those functions aren't typed
Multihashing.functions = {
// identity
0x00: crypto.identity,
// sha1
0x11: crypto.sha1,
// sha2-256
0x12: crypto.sha2256,
// sha2-512
0x13: crypto.sha2512,
// sha3-512
0x14: crypto.sha3512,
// sha3-384
0x15: crypto.sha3384,
// sha3-256
0x16: crypto.sha3256,
// sha3-224
0x17: crypto.sha3224,
// shake-128
0x18: crypto.shake128,
// shake-256
0x19: crypto.shake256,
// keccak-224
0x1A: crypto.keccak224,
// keccak-256
0x1B: crypto.keccak256,
// keccak-384
0x1C: crypto.keccak384,
// keccak-512
0x1D: crypto.keccak512,
// murmur3-128
0x22: crypto.murmur3128,
// murmur3-32
0x23: crypto.murmur332,
// dbl-sha2-256
0x56: crypto.dblSha2256
}
// add blake functions
crypto.addBlake(Multihashing.functions)
/**
* @param {Uint8Array} bytes
* @param {Uint8Array} hash
* @returns {Promise<boolean>}
*/
Multihashing.validate = async (bytes, hash) => {
const newHash = await Multihashing(bytes, multihash.decode(hash).name)
return equals(hash, newHash)
}
module.exports = Multihashing