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

Commit

Permalink
feat: change api to async/await (#37)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: callbacks are not supported!!
  • Loading branch information
hugomrdias committed Apr 16, 2019
1 parent 11f9b87 commit 01f1da6
Show file tree
Hide file tree
Showing 13 changed files with 250 additions and 312 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2016 Protocol Labs Inc.
Copyright (c) Protocol Labs Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
29 changes: 13 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,37 +88,34 @@ You will need to use Node.js `Buffer` API compatible, if you are running inside
const multihashing = require('multihashing-async')
const buf = Buffer.from('beep boop')

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

// Use `.digest(...)` if you want only the hash digest (drops the prefix indicating the hash type).
multihashing.digest(buf, 'sha1', (err , digest) => {
// digest is the raw digest
})
const digest = await multihashing.digest(buf, 'sha1')

// Use `.createHash(...)` for the raw hash functions
const h = multihashing.createHash('sha1')
h(buf, (err, digest) => {
// digest is a buffer of the sha1 of buf
})
const hash = multihashing.createHash('sha1')
const digest = await hash(buf)
```

## Examples

### Multihash output

```js
> const multihashing = require('multihashing-async')
> const buf = Buffer.from('beep boop')
const multihashing = require('multihashing-async')
const buf = Buffer.from('beep boop')

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

> multihashing(buf, 'sha2-256', (err, mh) => console.log(mh))
const mh = await multihashing(buf, 'sha2-256')
console.log(mh)
// => <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>

> multihashing(buf, 'sha2-512', (err, mh) => console.log(mh))
const mh = await multihashing(buf, 'sha2-512')
console.log(mh)
// => <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 ...>
```

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

## License

[MIT](LICENSE) © 2016 Protocol Labs Inc.
[MIT](LICENSE) © Protocol Labs Inc.
17 changes: 9 additions & 8 deletions benchmarks/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,20 @@ const algs = [
'keccak-384',
'keccak-512',
'murmur3-32',
'murmur3-128'
'murmur3-128',
'dbl-sha2-256',
'blake2b-256',
'blake2b-512',
'blake2s-256'
]

algs.forEach((alg) => {
suite.add(alg, function (d) {
suite.add(alg, async function (d) {
const buf = Buffer.alloc(10 * 1024)
buf.fill(Math.ceil(Math.random() * 100))

multihashing(buf, alg, (err, res) => {
if (err) throw err
list.push(res)
d.resolve()
})
const res = await multihashing(buf, alg)
list.push(res)
d.resolve()
}, {
defer: true
})
Expand Down
19 changes: 12 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
"version": "0.6.0",
"description": "multiple hash functions",
"keywords": [
"multihash"
"multihash",
"hash",
"hashing",
"async"
],
"homepage": "https://github.com/multiformats/js-multihashing-async",
"bugs": "https://github.com/multiformats/js-multihashing-async/issues",
"license": "MIT",
"leadMaintainer": "Hugo Dias <mail@hugodias.me>",
"leadMaintainer": "Hugo Dias <hugomrdias@gmail.com>",
"files": [
"src",
"dist"
],
"main": "src/index.js",
"browser": {
"./src/crypto-sha1-2.js": "./src/crypto-sha1-2-browser.js"
"./src/sha.js": "./src/sha.browser.js"
},
"repository": "github:multiformats/js-multihashing-async",
"scripts": {
Expand All @@ -32,16 +35,18 @@
},
"dependencies": {
"blakejs": "^1.1.0",
"buffer": "^5.2.1",
"err-code": "^1.1.2",
"js-sha3": "~0.8.0",
"multihashes": "~0.4.13",
"murmurhash3js": "^3.0.1",
"nodeify": "^1.0.1"
"murmurhash3js-revisited": "^3.0.0"
},
"devDependencies": {
"aegir": "^18.0.3",
"aegir": "^18.2.2",
"benchmark": "^2.1.4",
"chai": "^4.1.2",
"dirty-chai": "^2.0.1"
"dirty-chai": "^2.0.1",
"sinon": "^7.2.7"
},
"engines": {
"node": ">=6.0.0",
Expand Down
12 changes: 7 additions & 5 deletions src/blake.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use strict'

const { Buffer } = require('buffer')
const blake = require('blakejs')

const toCallback = require('./utils').toCallback

const minB = 0xb201
const minS = 0xb241

Expand All @@ -19,11 +18,14 @@ const blake2s = {
digest: blake.blake2sFinal
}

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

module.exports = (table) => {
for (let i = 0; i < 64; i++) {
Expand Down
60 changes: 0 additions & 60 deletions src/crypto-sha1-2-browser.js

This file was deleted.

22 changes: 0 additions & 22 deletions src/crypto-sha1-2.js

This file was deleted.

85 changes: 53 additions & 32 deletions src/crypto.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,62 @@
'use strict'

const { Buffer } = require('buffer')
const sha3 = require('js-sha3')
const murmur3 = require('murmurhash3js')
const mur = require('murmurhash3js-revisited')
const sha = require('./sha')
const { fromNumberTo32BitBuf } = require('./utils')

const utils = require('./utils')
const sha = require('./crypto-sha1-2')
// Note that although this function doesn't do any asynchronous work, we mark
// the function as async because it must return a Promise to match the API
// for other functions that do perform asynchronous work (see sha.browser.js)
const hash = (algorithm) => async (data) => {
switch (algorithm) {
case 'sha3-224':
return Buffer.from(sha3.sha3_224.arrayBuffer(data))
case 'sha3-256':
return Buffer.from(sha3.sha3_256.arrayBuffer(data))
case 'sha3-384':
return Buffer.from(sha3.sha3_384.arrayBuffer(data))
case 'sha3-512':
return Buffer.from(sha3.sha3_512.arrayBuffer(data))
case 'shake-128':
return Buffer.from(sha3.shake128.create(128).update(data).arrayBuffer())
case 'shake-256':
return Buffer.from(sha3.shake256.create(256).update(data).arrayBuffer())
case 'keccak-224':
return Buffer.from(sha3.keccak224.arrayBuffer(data))
case 'keccak-256':
return Buffer.from(sha3.keccak256.arrayBuffer(data))
case 'keccak-384':
return Buffer.from(sha3.keccak384.arrayBuffer(data))
case 'keccak-512':
return Buffer.from(sha3.keccak512.arrayBuffer(data))
case 'murmur3-128':
return Buffer.from(mur.x64.hash128(data), 'hex')
case 'murmur3-32':
return fromNumberTo32BitBuf(mur.x86.hash32(data))

const toCallback = utils.toCallback
const toBuf = utils.toBuf
const fromString = utils.fromString
const fromNumberTo32BitBuf = utils.fromNumberTo32BitBuf

const dblSha2256 = (buf, cb) => {
sha.sha2256(buf, (err, firstHash) => {
if (err) {
cb(err)
}
sha.sha2256((Buffer.from(firstHash)), cb)
})
default:
throw new TypeError(`${algorithm} is not a supported algorithm`)
}
}

module.exports = {
sha1: sha.sha1,
sha2256: sha.sha2256,
sha2512: sha.sha2512,
sha3512: toCallback(toBuf(sha3.sha3_512)),
sha3384: toCallback(toBuf(sha3.sha3_384)),
sha3256: toCallback(toBuf(sha3.sha3_256)),
sha3224: toCallback(toBuf(sha3.sha3_224)),
shake128: toCallback(toBuf(sha3.shake_128, 128)),
shake256: toCallback(toBuf(sha3.shake_256, 256)),
keccak224: toCallback(toBuf(sha3.keccak_224)),
keccak256: toCallback(toBuf(sha3.keccak_256)),
keccak384: toCallback(toBuf(sha3.keccak_384)),
keccak512: toCallback(toBuf(sha3.keccak_512)),
murmur3128: toCallback(toBuf(fromString(murmur3.x64.hash128))),
murmur332: toCallback(fromNumberTo32BitBuf(fromString(murmur3.x86.hash32))),
addBlake: require('./blake'),
dblSha2256: dblSha2256
sha1: sha('sha1'),
sha2256: sha('sha2-256'),
sha2512: sha('sha2-512'),
dblSha2256: sha('dbl-sha2-256'),
sha3224: hash('sha3-224'),
sha3256: hash('sha3-256'),
sha3384: hash('sha3-384'),
sha3512: hash('sha3-512'),
shake128: hash('shake-128'),
shake256: hash('shake-256'),
keccak224: hash('keccak-224'),
keccak256: hash('keccak-256'),
keccak384: hash('keccak-384'),
keccak512: hash('keccak-512'),
murmur3128: hash('murmur3-128'),
murmur332: hash('murmur3-32'),
addBlake: require('./blake')
}
Loading

0 comments on commit 01f1da6

Please sign in to comment.