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

Commit d6889e9

Browse files
committed
fix: add validate function and more
1 parent bf59286 commit d6889e9

File tree

8 files changed

+29
-29
lines changed

8 files changed

+29
-29
lines changed

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,18 @@
3535
},
3636
"dependencies": {
3737
"blakejs": "^1.1.0",
38+
"buffer": "^5.2.1",
3839
"err-code": "^1.1.2",
3940
"js-sha3": "~0.8.0",
4041
"multihashes": "~0.4.13",
4142
"murmurhash3js-revisited": "^3.0.0"
4243
},
4344
"devDependencies": {
44-
"sinon": "^7.2.7",
45-
"aegir": "^18.0.3",
45+
"aegir": "^18.2.2",
4646
"benchmark": "^2.1.4",
4747
"chai": "^4.1.2",
48-
"dirty-chai": "^2.0.1"
48+
"dirty-chai": "^2.0.1",
49+
"sinon": "^7.2.7"
4950
},
5051
"engines": {
5152
"node": ">=6.0.0",

src/blake.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const { Buffer } = require('buffer')
34
const blake = require('blakejs')
45

56
const minB = 0xb201

src/crypto.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const { Buffer } = require('buffer')
34
const sha3 = require('js-sha3')
45
const mur = require('murmurhash3js-revisited')
56
const sha = require('./sha')

src/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const { Buffer } = require('buffer')
34
const errcode = require('err-code')
45
const multihash = require('multihashes')
56
const crypto = require('./crypto')
@@ -102,11 +103,10 @@ Multihashing.functions = {
102103
// add blake functions
103104
crypto.addBlake(Multihashing.functions)
104105

105-
Multihashing.validate = (data, hash, callback) => {
106-
let algo = multihash.decode(hash).name
107-
Multihashing(data, algo, (err, newHash) => {
108-
if (err) return callback(err)
109-
callback(err, Buffer.compare(hash, newHash) === 0)
110-
})
106+
Multihashing.validate = async (buf, hash) => {
107+
const newHash = await Multihashing(buf, multihash.decode(hash).name)
108+
109+
return Buffer.compare(hash, newHash) === 0
111110
}
111+
112112
module.exports = Multihashing

src/sha.browser.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
'use strict'
2+
3+
const { Buffer } = require('buffer')
4+
25
const crypto = self.crypto || self.msCrypto
36

47
module.exports = (algorithm) => {
@@ -21,7 +24,7 @@ module.exports = (algorithm) => {
2124
return Buffer.from(await crypto.subtle.digest({ name: 'SHA-256' }, d))
2225
}
2326
default:
24-
throw new TypeError(`${algorithm} is not a supported algorithm`)
27+
throw new Error(`${algorithm} is not a supported algorithm`)
2528
}
2629
}
2730
}

src/sha.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ module.exports = (algorithm) => async (data) => {
1717
return crypto.createHash('sha256').update(first).digest()
1818
}
1919
default:
20-
throw new TypeError(`${algorithm} is not a supported algorithm`)
20+
throw new Error(`${algorithm} is not a supported algorithm`)
2121
}
2222
}

src/utils.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict'
22

3+
const { Buffer } = require('buffer')
4+
35
const fromNumberTo32BitBuf = (number) => {
46
const bytes = new Array(4)
57

test/index.spec.js

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-env mocha */
22
'use strict'
33

4+
const { Buffer } = require('buffer')
45
const chai = require('chai')
56
const dirtyChai = require('dirty-chai')
67
chai.use(dirtyChai)
@@ -43,26 +44,17 @@ describe('multihashing', () => {
4344
})
4445

4546
describe('validate', () => {
46-
it('true on pass', done => {
47-
multihashing(Buffer.from('test'), 'sha2-256', (err, hash) => {
48-
if (err) throw done(err)
49-
multihashing.validate(Buffer.from('test'), hash, (err, bool) => {
50-
if (err) throw done(err)
51-
expect(bool).to.eql(true)
52-
done()
53-
})
54-
})
47+
it('true on pass', async () => {
48+
const hash = await multihashing(Buffer.from('test'), 'sha2-256')
49+
const validation = await multihashing.validate(Buffer.from('test'), hash)
50+
51+
return expect(validation).to.eql(true)
5552
})
5653

57-
it('false on fail', done => {
58-
multihashing(Buffer.from('test'), 'sha2-256', (err, hash) => {
59-
if (err) throw done(err)
60-
multihashing.validate(Buffer.from('test-fail'), hash, (err, bool) => {
61-
if (err) throw done(err)
62-
expect(bool).to.eql(false)
63-
done()
64-
})
65-
})
54+
it('false on fail', async () => {
55+
const hash = await multihashing(Buffer.from('test'), 'sha2-256')
56+
const validation = await multihashing.validate(Buffer.from('test-fail'), hash)
57+
return expect(validation).to.eql(false)
6658
})
6759
})
6860

0 commit comments

Comments
 (0)