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

Commit

Permalink
feat: add .verify (#26)
Browse files Browse the repository at this point in the history
* feat: add .verify

* docs: add readme links
  • Loading branch information
hacdias authored and daviddias committed Jan 9, 2018
1 parent 6769450 commit fcaa241
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ For now, it just uses `crypto`, but will use `sha3` and `blake2`, etc.
- [Examples](#examples)
- [Multihash output](#multihash-output)
- [Raw digest output](#raw-digest-output)
- [Verify a multihash](#verify-a-multihash)
- [API](#api)
- [`multihashing(buf, func, length)`](#multihashingbuf-func-length)
- [`digest(buf, func, length)`](#digestbuf-func-length)
- [`createHash(func, length)`](#createhashfunc-length)
- [`verify(input, buf)`](#verifyhash-buf)
- [`functions`](#functions)
- [Maintainers](#maintainers)
- [Contribute](#contribute)
Expand Down Expand Up @@ -126,6 +128,20 @@ h.digest()
// => <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 ...>
```

### Verify a multihash

```js
> const right = new Buffer('beep boop')
> const wrong = new Buffer('oops')
> const hash = multihashing(right, 'sha1')

> console.log(multihashing.verify(hash, right))
// => true

> console.log(multihashing.verify(hash, wrong))
// => false
```

## API

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

### `createHash(func, length)`

### `verify(hash, buf)`

### `functions`

An object mapping hexcodes to their hash functions.
Expand Down
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ mh.createHash = function (func, length) {
return mh.functions[func]()
}

mh.verify = function (hash, buf) {
const decoded = multihash.decode(hash)
const encoded = mh(buf, decoded.name, decoded.length)
return encoded.equals(hash)
}

mh.functions = {
0x11: gsha1,
0x12: gsha2256,
Expand Down
1 change: 1 addition & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ describe('multihashing', () => {
const input = Buffer.from(test[0])
const output = Buffer.from(test[1], 'hex')
expect(multihashing(input, algo)).to.be.eql(output)
expect(multihashing.verify(output, input)).to.be.eql(true)
}
})
}
Expand Down

0 comments on commit fcaa241

Please sign in to comment.