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

Commit dbe2a4a

Browse files
authored
fix: expose sha (#66)
1 parent 206433f commit dbe2a4a

File tree

4 files changed

+44
-21
lines changed

4 files changed

+44
-21
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"murmurhash3js-revisited": "^3.0.0"
4343
},
4444
"devDependencies": {
45-
"aegir": "^20.1.0",
45+
"aegir": "^21.3.0",
4646
"benchmark": "^2.1.4",
4747
"chai": "^4.1.2",
4848
"dirty-chai": "^2.0.1",

src/crypto.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const { Buffer } = require('buffer')
44
const sha3 = require('js-sha3')
55
const mur = require('murmurhash3js-revisited')
6-
const sha = require('./sha')
6+
const { factory: sha } = require('./sha')
77
const { fromNumberTo32BitBuf } = require('./utils')
88

99
// Note that although this function doesn't do any asynchronous work, we mark

src/sha.browser.js

+26-16
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,40 @@
1+
/* eslint-disable require-await */
12
'use strict'
23

34
const { Buffer } = require('buffer')
5+
const multihash = require('multihashes')
46

57
const crypto = self.crypto || self.msCrypto
68

7-
module.exports = (algorithm) => {
9+
const digest = async (data, alg) => {
810
if (typeof self === 'undefined' || (!self.crypto && !self.msCrypto)) {
911
throw new Error(
1012
'Please use a browser with webcrypto support and ensure the code has been delivered securely via HTTPS/TLS and run within a Secure Context'
1113
)
1214
}
13-
14-
return async (data) => {
15-
switch (algorithm) {
16-
case 'sha1':
17-
return Buffer.from(await crypto.subtle.digest({ name: 'SHA-1' }, data))
18-
case 'sha2-256':
19-
return Buffer.from(await crypto.subtle.digest({ name: 'SHA-256' }, data))
20-
case 'sha2-512':
21-
return Buffer.from(await crypto.subtle.digest({ name: 'SHA-512' }, data))
22-
case 'dbl-sha2-256': {
23-
const d = await crypto.subtle.digest({ name: 'SHA-256' }, data)
24-
return Buffer.from(await crypto.subtle.digest({ name: 'SHA-256' }, d))
25-
}
26-
default:
27-
throw new Error(`${algorithm} is not a supported algorithm`)
15+
switch (alg) {
16+
case 'sha1':
17+
return Buffer.from(await crypto.subtle.digest({ name: 'SHA-1' }, data))
18+
case 'sha2-256':
19+
return Buffer.from(await crypto.subtle.digest({ name: 'SHA-256' }, data))
20+
case 'sha2-512':
21+
return Buffer.from(await crypto.subtle.digest({ name: 'SHA-512' }, data))
22+
case 'dbl-sha2-256': {
23+
const d = await crypto.subtle.digest({ name: 'SHA-256' }, data)
24+
return Buffer.from(await crypto.subtle.digest({ name: 'SHA-256' }, d))
2825
}
26+
default:
27+
throw new Error(`${alg} is not a supported algorithm`)
28+
}
29+
}
30+
31+
module.exports = {
32+
factory: (alg) => async (data) => {
33+
return digest(data, alg)
34+
},
35+
digest,
36+
multihashing: async (buf, alg, length) => {
37+
const h = await digest(buf, alg, length)
38+
return multihash.encode(h, alg, length)
2939
}
3040
}

src/sha.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
/* eslint-disable require-await */
12
'use strict'
23
const crypto = require('crypto')
4+
const multihash = require('multihashes')
35

46
// Note that although this function doesn't do any asynchronous work, we mark
57
// the function as async because it must return a Promise to match the API
68
// for other functions that do perform asynchronous work (see sha.browser.js)
79
// eslint-disable-next-line
8-
module.exports = (algorithm) => async (data) => {
9-
switch (algorithm) {
10+
const digest = async (data, alg) => {
11+
switch (alg) {
1012
case 'sha1':
1113
return crypto.createHash('sha1').update(data).digest()
1214
case 'sha2-256':
@@ -18,6 +20,17 @@ module.exports = (algorithm) => async (data) => {
1820
return crypto.createHash('sha256').update(first).digest()
1921
}
2022
default:
21-
throw new Error(`${algorithm} is not a supported algorithm`)
23+
throw new Error(`${alg} is not a supported algorithm`)
24+
}
25+
}
26+
27+
module.exports = {
28+
factory: (alg) => async (data) => {
29+
return digest(data, alg)
30+
},
31+
digest,
32+
multihashing: async (buf, alg, length) => {
33+
const h = await digest(buf, alg, length)
34+
return multihash.encode(h, alg, length)
2235
}
2336
}

0 commit comments

Comments
 (0)