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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: change api to async/await (#37)
BREAKING CHANGE: callbacks are not supported!!
- Loading branch information
1 parent
11f9b87
commit 01f1da6
Showing
13 changed files
with
250 additions
and
312 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |
Oops, something went wrong.