Skip to content
This repository was archived by the owner on Aug 24, 2021. It is now read-only.

Commit 01f1da6

Browse files
authored
feat: change api to async/await (#37)
BREAKING CHANGE: callbacks are not supported!!
1 parent 11f9b87 commit 01f1da6

13 files changed

+250
-312
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2016 Protocol Labs Inc.
3+
Copyright (c) Protocol Labs Inc.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+13-16
Original file line numberDiff line numberDiff line change
@@ -88,37 +88,34 @@ You will need to use Node.js `Buffer` API compatible, if you are running inside
8888
const multihashing = require('multihashing-async')
8989
const buf = Buffer.from('beep boop')
9090

91-
multihashing(buf, 'sha1', (err, multihash) => {
92-
// by default calls back with a multihash.
93-
})
91+
const mh = await multihashing(buf, 'sha1')
9492

9593
// Use `.digest(...)` if you want only the hash digest (drops the prefix indicating the hash type).
96-
multihashing.digest(buf, 'sha1', (err , digest) => {
97-
// digest is the raw digest
98-
})
94+
const digest = await multihashing.digest(buf, 'sha1')
9995

10096
// Use `.createHash(...)` for the raw hash functions
101-
const h = multihashing.createHash('sha1')
102-
h(buf, (err, digest) => {
103-
// digest is a buffer of the sha1 of buf
104-
})
97+
const hash = multihashing.createHash('sha1')
98+
const digest = await hash(buf)
10599
```
106100

107101
## Examples
108102

109103
### Multihash output
110104

111105
```js
112-
> const multihashing = require('multihashing-async')
113-
> const buf = Buffer.from('beep boop')
106+
const multihashing = require('multihashing-async')
107+
const buf = Buffer.from('beep boop')
114108

115-
> multihashing(buf, 'sha1', (err, mh) => console.log(mh))
109+
const mh = await multihashing(buf, 'sha1')
110+
console.log(mh)
116111
// => <Buffer 11 14 7c 83 57 57 7f 51 d4 f0 a8 d3 93 aa 1a aa fb 28 86 3d 94 21>
117112

118-
> multihashing(buf, 'sha2-256', (err, mh) => console.log(mh))
113+
const mh = await multihashing(buf, 'sha2-256')
114+
console.log(mh)
119115
// => <Buffer 12 20 90 ea 68 8e 27 5d 58 05 67 32 50 32 49 2b 59 7b c7 72 21 c6 24 93 e7 63 30 b8 5d dd a1 91 ef 7c>
120116

121-
> multihashing(buf, 'sha2-512', (err, mh) => console.log(mh))
117+
const mh = await multihashing(buf, 'sha2-512')
118+
console.log(mh)
122119
// => <Buffer 13 40 14 f3 01 f3 1b e2 43 f3 4c 56 68 93 78 83 77 1f a3 81 00 2f 1a aa 5f 31 b3 f7 8e 50 0b 66 ff 2f 4f 8e a5 e3 c9 f5 a6 1b d0 73 e2 45 2c 48 04 84 b0 ...>
123120
```
124121

@@ -137,4 +134,4 @@ Small note: If editing the README, please conform to the [standard-readme](https
137134

138135
## License
139136

140-
[MIT](LICENSE) © 2016 Protocol Labs Inc.
137+
[MIT](LICENSE) © Protocol Labs Inc.

benchmarks/hash.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,20 @@ const algs = [
2222
'keccak-384',
2323
'keccak-512',
2424
'murmur3-32',
25-
'murmur3-128'
25+
'murmur3-128',
26+
'dbl-sha2-256',
27+
'blake2b-256',
28+
'blake2b-512',
29+
'blake2s-256'
2630
]
2731

2832
algs.forEach((alg) => {
29-
suite.add(alg, function (d) {
33+
suite.add(alg, async function (d) {
3034
const buf = Buffer.alloc(10 * 1024)
3135
buf.fill(Math.ceil(Math.random() * 100))
32-
33-
multihashing(buf, alg, (err, res) => {
34-
if (err) throw err
35-
list.push(res)
36-
d.resolve()
37-
})
36+
const res = await multihashing(buf, alg)
37+
list.push(res)
38+
d.resolve()
3839
}, {
3940
defer: true
4041
})

package.json

+12-7
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,22 @@
33
"version": "0.6.0",
44
"description": "multiple hash functions",
55
"keywords": [
6-
"multihash"
6+
"multihash",
7+
"hash",
8+
"hashing",
9+
"async"
710
],
811
"homepage": "https://github.com/multiformats/js-multihashing-async",
912
"bugs": "https://github.com/multiformats/js-multihashing-async/issues",
1013
"license": "MIT",
11-
"leadMaintainer": "Hugo Dias <[email protected]>",
14+
"leadMaintainer": "Hugo Dias <[email protected]>",
1215
"files": [
1316
"src",
1417
"dist"
1518
],
1619
"main": "src/index.js",
1720
"browser": {
18-
"./src/crypto-sha1-2.js": "./src/crypto-sha1-2-browser.js"
21+
"./src/sha.js": "./src/sha.browser.js"
1922
},
2023
"repository": "github:multiformats/js-multihashing-async",
2124
"scripts": {
@@ -32,16 +35,18 @@
3235
},
3336
"dependencies": {
3437
"blakejs": "^1.1.0",
38+
"buffer": "^5.2.1",
39+
"err-code": "^1.1.2",
3540
"js-sha3": "~0.8.0",
3641
"multihashes": "~0.4.13",
37-
"murmurhash3js": "^3.0.1",
38-
"nodeify": "^1.0.1"
42+
"murmurhash3js-revisited": "^3.0.0"
3943
},
4044
"devDependencies": {
41-
"aegir": "^18.0.3",
45+
"aegir": "^18.2.2",
4246
"benchmark": "^2.1.4",
4347
"chai": "^4.1.2",
44-
"dirty-chai": "^2.0.1"
48+
"dirty-chai": "^2.0.1",
49+
"sinon": "^7.2.7"
4550
},
4651
"engines": {
4752
"node": ">=6.0.0",

src/blake.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
'use strict'
22

3+
const { Buffer } = require('buffer')
34
const blake = require('blakejs')
45

5-
const toCallback = require('./utils').toCallback
6-
76
const minB = 0xb201
87
const minS = 0xb241
98

@@ -19,11 +18,14 @@ const blake2s = {
1918
digest: blake.blake2sFinal
2019
}
2120

22-
const makeB2Hash = (size, hf) => toCallback((buf) => {
21+
// Note that although this function doesn't do any asynchronous work, we mark
22+
// the function as async because it must return a Promise to match the API
23+
// for other functions that do perform asynchronous work (see sha.browser.js)
24+
const makeB2Hash = (size, hf) => async (data) => {
2325
const ctx = hf.init(size, null)
24-
hf.update(ctx, buf)
26+
hf.update(ctx, data)
2527
return Buffer.from(hf.digest(ctx))
26-
})
28+
}
2729

2830
module.exports = (table) => {
2931
for (let i = 0; i < 64; i++) {

src/crypto-sha1-2-browser.js

-60
This file was deleted.

src/crypto-sha1-2.js

-22
This file was deleted.

src/crypto.js

+53-32
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,62 @@
11
'use strict'
22

3+
const { Buffer } = require('buffer')
34
const sha3 = require('js-sha3')
4-
const murmur3 = require('murmurhash3js')
5+
const mur = require('murmurhash3js-revisited')
6+
const sha = require('./sha')
7+
const { fromNumberTo32BitBuf } = require('./utils')
58

6-
const utils = require('./utils')
7-
const sha = require('./crypto-sha1-2')
9+
// Note that although this function doesn't do any asynchronous work, we mark
10+
// the function as async because it must return a Promise to match the API
11+
// for other functions that do perform asynchronous work (see sha.browser.js)
12+
const hash = (algorithm) => async (data) => {
13+
switch (algorithm) {
14+
case 'sha3-224':
15+
return Buffer.from(sha3.sha3_224.arrayBuffer(data))
16+
case 'sha3-256':
17+
return Buffer.from(sha3.sha3_256.arrayBuffer(data))
18+
case 'sha3-384':
19+
return Buffer.from(sha3.sha3_384.arrayBuffer(data))
20+
case 'sha3-512':
21+
return Buffer.from(sha3.sha3_512.arrayBuffer(data))
22+
case 'shake-128':
23+
return Buffer.from(sha3.shake128.create(128).update(data).arrayBuffer())
24+
case 'shake-256':
25+
return Buffer.from(sha3.shake256.create(256).update(data).arrayBuffer())
26+
case 'keccak-224':
27+
return Buffer.from(sha3.keccak224.arrayBuffer(data))
28+
case 'keccak-256':
29+
return Buffer.from(sha3.keccak256.arrayBuffer(data))
30+
case 'keccak-384':
31+
return Buffer.from(sha3.keccak384.arrayBuffer(data))
32+
case 'keccak-512':
33+
return Buffer.from(sha3.keccak512.arrayBuffer(data))
34+
case 'murmur3-128':
35+
return Buffer.from(mur.x64.hash128(data), 'hex')
36+
case 'murmur3-32':
37+
return fromNumberTo32BitBuf(mur.x86.hash32(data))
838

9-
const toCallback = utils.toCallback
10-
const toBuf = utils.toBuf
11-
const fromString = utils.fromString
12-
const fromNumberTo32BitBuf = utils.fromNumberTo32BitBuf
13-
14-
const dblSha2256 = (buf, cb) => {
15-
sha.sha2256(buf, (err, firstHash) => {
16-
if (err) {
17-
cb(err)
18-
}
19-
sha.sha2256((Buffer.from(firstHash)), cb)
20-
})
39+
default:
40+
throw new TypeError(`${algorithm} is not a supported algorithm`)
41+
}
2142
}
2243

2344
module.exports = {
24-
sha1: sha.sha1,
25-
sha2256: sha.sha2256,
26-
sha2512: sha.sha2512,
27-
sha3512: toCallback(toBuf(sha3.sha3_512)),
28-
sha3384: toCallback(toBuf(sha3.sha3_384)),
29-
sha3256: toCallback(toBuf(sha3.sha3_256)),
30-
sha3224: toCallback(toBuf(sha3.sha3_224)),
31-
shake128: toCallback(toBuf(sha3.shake_128, 128)),
32-
shake256: toCallback(toBuf(sha3.shake_256, 256)),
33-
keccak224: toCallback(toBuf(sha3.keccak_224)),
34-
keccak256: toCallback(toBuf(sha3.keccak_256)),
35-
keccak384: toCallback(toBuf(sha3.keccak_384)),
36-
keccak512: toCallback(toBuf(sha3.keccak_512)),
37-
murmur3128: toCallback(toBuf(fromString(murmur3.x64.hash128))),
38-
murmur332: toCallback(fromNumberTo32BitBuf(fromString(murmur3.x86.hash32))),
39-
addBlake: require('./blake'),
40-
dblSha2256: dblSha2256
45+
sha1: sha('sha1'),
46+
sha2256: sha('sha2-256'),
47+
sha2512: sha('sha2-512'),
48+
dblSha2256: sha('dbl-sha2-256'),
49+
sha3224: hash('sha3-224'),
50+
sha3256: hash('sha3-256'),
51+
sha3384: hash('sha3-384'),
52+
sha3512: hash('sha3-512'),
53+
shake128: hash('shake-128'),
54+
shake256: hash('shake-256'),
55+
keccak224: hash('keccak-224'),
56+
keccak256: hash('keccak-256'),
57+
keccak384: hash('keccak-384'),
58+
keccak512: hash('keccak-512'),
59+
murmur3128: hash('murmur3-128'),
60+
murmur332: hash('murmur3-32'),
61+
addBlake: require('./blake')
4162
}

0 commit comments

Comments
 (0)