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

Commit d92fe85

Browse files
committed
feat: validation. fixes #43
1 parent 0438ee7 commit d92fe85

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,12 @@ Multihashing.functions = {
139139

140140
// add blake functions
141141
crypto.addBlake(Multihashing.functions)
142+
143+
Multihashing.validate = (data, hash, callback) => {
144+
let algo = multihash.decode(hash).name
145+
Multihashing(data, algo, (err, newHash) => {
146+
if (err) return callback(err)
147+
callback(err, Buffer.compare(hash, newHash) === 0)
148+
})
149+
}
150+

test/index.spec.js

+26
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,29 @@ describe('multihashing', () => {
7676
})
7777
})
7878
})
79+
80+
describe('validate', () => {
81+
it('true on pass', done => {
82+
multihashing(Buffer.from('test'), 'sha2-256', (err, hash) => {
83+
if (err) throw err
84+
multihashing.validate(Buffer.from('test'), hash, (err, bool) => {
85+
if (err) throw err
86+
console.error('asdf', bool)
87+
expect(bool).to.eql(true)
88+
done()
89+
})
90+
})
91+
})
92+
93+
it('false on fail', done => {
94+
multihashing(Buffer.from('test'), 'sha2-256', (err, hash) => {
95+
if (err) throw err
96+
multihashing.validate(Buffer.from('test-fail'), hash, (err, bool) => {
97+
if (err) throw err
98+
expect(bool).to.eql(false)
99+
done()
100+
})
101+
})
102+
})
103+
})
104+

0 commit comments

Comments
 (0)