-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathmultihash-interface.ts
54 lines (42 loc) · 1.52 KB
/
multihash-interface.ts
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
import { CID } from 'multiformats/cid'
import crypto from 'crypto'
import * as json from 'multiformats/codecs/json'
import * as hasher from 'multiformats/hashes/hasher'
// ** Example 1: sha2-256 hasher **
const sha256 = hasher.from({
// As per multiformats table
// https://github.com/multiformats/multicodec/blob/master/table.csv#L9
name: 'sha2-256',
code: 0x12,
encode: (input) => new Uint8Array(crypto.createHash('sha256').update(input).digest())
})
async function run1 () {
const hash = await sha256.digest(json.encode({ hello: 'world' }))
const cid = CID.create(1, json.code, hash)
console.log(cid.multihash.size) // should equal 32 bytes for sha256
console.log(cid)
// CID(bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea)
}
run1().catch((err) => {
console.error(err)
process.exit(1)
})
// ** Example 2: sha3-512 hasher **
const sha512 = hasher.from({
// As per multiformats table
// https://github.com/multiformats/multicodec/blob/master/table.csv#L9
name: 'sha3-512',
code: 0x14,
encode: (input) => new Uint8Array(crypto.createHash('sha512').update(input).digest())
})
async function run2 () {
const hash2 = await sha512.digest(json.encode({ hello: 'world' }))
const cid2 = CID.create(1, json.code, hash2)
console.log(cid2.multihash.size) // should equal 64 bytes for sha512
console.log(cid2)
// CID(bagaaifca7d5wrebdi6rmqkgtrqyodq3bo6gitrqtemxtliymakwswbazbu7ai763747ljp7ycqfv7aqx4xlgiugcx62quo2te45pcgjbg4qjsvq)
}
run2().catch((err) => {
console.error(err)
process.exit(1)
})