From 1aaab5b42b95bf39883791ad0775d80b8dc6fc81 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Sat, 14 Mar 2020 19:44:33 +0000 Subject: [PATCH] fix: expose sha --- package.json | 2 +- src/crypto.js | 2 +- src/sha.browser.js | 42 ++++++++++++++++++++++++++---------------- src/sha.js | 19 ++++++++++++++++--- 4 files changed, 44 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index b8cf60e6..d3d52a5b 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "murmurhash3js-revisited": "^3.0.0" }, "devDependencies": { - "aegir": "^20.1.0", + "aegir": "^21.3.0", "benchmark": "^2.1.4", "chai": "^4.1.2", "dirty-chai": "^2.0.1", diff --git a/src/crypto.js b/src/crypto.js index 08041a16..c53674ce 100644 --- a/src/crypto.js +++ b/src/crypto.js @@ -3,7 +3,7 @@ const { Buffer } = require('buffer') const sha3 = require('js-sha3') const mur = require('murmurhash3js-revisited') -const sha = require('./sha') +const { factory: sha } = require('./sha') const { fromNumberTo32BitBuf } = require('./utils') // Note that although this function doesn't do any asynchronous work, we mark diff --git a/src/sha.browser.js b/src/sha.browser.js index 996eba08..f9e548e5 100644 --- a/src/sha.browser.js +++ b/src/sha.browser.js @@ -1,30 +1,40 @@ +/* eslint-disable require-await */ 'use strict' const { Buffer } = require('buffer') +const multihash = require('multihashes') const crypto = self.crypto || self.msCrypto -module.exports = (algorithm) => { +const digest = async (data, alg) => { if (typeof self === 'undefined' || (!self.crypto && !self.msCrypto)) { throw new Error( 'Please use a browser with webcrypto support and ensure the code has been delivered securely via HTTPS/TLS and run within a Secure Context' ) } - - return async (data) => { - switch (algorithm) { - case 'sha1': - return Buffer.from(await crypto.subtle.digest({ name: 'SHA-1' }, data)) - case 'sha2-256': - return Buffer.from(await crypto.subtle.digest({ name: 'SHA-256' }, data)) - case 'sha2-512': - return Buffer.from(await crypto.subtle.digest({ name: 'SHA-512' }, data)) - case 'dbl-sha2-256': { - const d = await crypto.subtle.digest({ name: 'SHA-256' }, data) - return Buffer.from(await crypto.subtle.digest({ name: 'SHA-256' }, d)) - } - default: - throw new Error(`${algorithm} is not a supported algorithm`) + switch (alg) { + case 'sha1': + return Buffer.from(await crypto.subtle.digest({ name: 'SHA-1' }, data)) + case 'sha2-256': + return Buffer.from(await crypto.subtle.digest({ name: 'SHA-256' }, data)) + case 'sha2-512': + return Buffer.from(await crypto.subtle.digest({ name: 'SHA-512' }, data)) + case 'dbl-sha2-256': { + const d = await crypto.subtle.digest({ name: 'SHA-256' }, data) + return Buffer.from(await crypto.subtle.digest({ name: 'SHA-256' }, d)) } + default: + throw new Error(`${alg} is not a supported algorithm`) + } +} + +module.exports = { + factory: (alg) => async (data) => { + return digest(data, alg) + }, + digest, + multihashing: async (buf, alg, length) => { + const h = await digest(buf, alg, length) + return multihash.encode(h, alg, length) } } diff --git a/src/sha.js b/src/sha.js index 26df43bc..ec499eb8 100644 --- a/src/sha.js +++ b/src/sha.js @@ -1,12 +1,14 @@ +/* eslint-disable require-await */ 'use strict' const crypto = require('crypto') +const multihash = require('multihashes') // 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) // eslint-disable-next-line -module.exports = (algorithm) => async (data) => { - switch (algorithm) { +const digest = async (data, alg) => { + switch (alg) { case 'sha1': return crypto.createHash('sha1').update(data).digest() case 'sha2-256': @@ -18,6 +20,17 @@ module.exports = (algorithm) => async (data) => { return crypto.createHash('sha256').update(first).digest() } default: - throw new Error(`${algorithm} is not a supported algorithm`) + throw new Error(`${alg} is not a supported algorithm`) + } +} + +module.exports = { + factory: (alg) => async (data) => { + return digest(data, alg) + }, + digest, + multihashing: async (buf, alg, length) => { + const h = await digest(buf, alg, length) + return multihash.encode(h, alg, length) } }