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

Commit fcaa241

Browse files
hacdiasdaviddias
authored andcommitted
feat: add .verify (#26)
* feat: add .verify * docs: add readme links
1 parent 6769450 commit fcaa241

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ For now, it just uses `crypto`, but will use `sha3` and `blake2`, etc.
3131
- [Examples](#examples)
3232
- [Multihash output](#multihash-output)
3333
- [Raw digest output](#raw-digest-output)
34+
- [Verify a multihash](#verify-a-multihash)
3435
- [API](#api)
3536
- [`multihashing(buf, func, length)`](#multihashingbuf-func-length)
3637
- [`digest(buf, func, length)`](#digestbuf-func-length)
3738
- [`createHash(func, length)`](#createhashfunc-length)
39+
- [`verify(input, buf)`](#verifyhash-buf)
3840
- [`functions`](#functions)
3941
- [Maintainers](#maintainers)
4042
- [Contribute](#contribute)
@@ -126,6 +128,20 @@ h.digest()
126128
// => <SlowBuffer 14 f3 01 f3 1b e2 43 f3 4c 56 68 93 78 83 77 1f a3 81 00 2f 1a aa 5f 31 b3 f7 8e 50 0b 66 ff 2f 4f 8e a5 e3 c9 f5 a6 1b d0 73 e2 45 2c 48 04 84 b0 2e 03 ...>
127129
```
128130

131+
### Verify a multihash
132+
133+
```js
134+
> const right = new Buffer('beep boop')
135+
> const wrong = new Buffer('oops')
136+
> const hash = multihashing(right, 'sha1')
137+
138+
> console.log(multihashing.verify(hash, right))
139+
// => true
140+
141+
> console.log(multihashing.verify(hash, wrong))
142+
// => false
143+
```
144+
129145
## API
130146

131147
### `multihashing(buf, func, length)`
@@ -134,6 +150,8 @@ h.digest()
134150

135151
### `createHash(func, length)`
136152

153+
### `verify(hash, buf)`
154+
137155
### `functions`
138156

139157
An object mapping hexcodes to their hash functions.

src/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ mh.createHash = function (func, length) {
3535
return mh.functions[func]()
3636
}
3737

38+
mh.verify = function (hash, buf) {
39+
const decoded = multihash.decode(hash)
40+
const encoded = mh(buf, decoded.name, decoded.length)
41+
return encoded.equals(hash)
42+
}
43+
3844
mh.functions = {
3945
0x11: gsha1,
4046
0x12: gsha2256,

test/index.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ describe('multihashing', () => {
6868
const input = Buffer.from(test[0])
6969
const output = Buffer.from(test[1], 'hex')
7070
expect(multihashing(input, algo)).to.be.eql(output)
71+
expect(multihashing.verify(output, input)).to.be.eql(true)
7172
}
7273
})
7374
}

0 commit comments

Comments
 (0)