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

fix: expose sha #66

Merged
merged 1 commit into from
Mar 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 26 additions & 16 deletions src/sha.browser.js
Original file line number Diff line number Diff line change
@@ -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)
}
}
19 changes: 16 additions & 3 deletions src/sha.js
Original file line number Diff line number Diff line change
@@ -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':
Expand All @@ -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)
}
}