Skip to content

Commit dfa18ba

Browse files
committed
docs: add example, fix up README
1 parent c4e7663 commit dfa18ba

File tree

4 files changed

+64
-12
lines changed

4 files changed

+64
-12
lines changed

README.md

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,55 @@
1-
# js-sha3
1+
# @multiformats/sha3
22

3-
Multiformats hash functions for SHA3.
3+
SHA3 (and related) multihash hashers for [multiformats](https://github.com/multiformats/js-multiformats).
4+
5+
`MultihashHashers`s are exported from this library, they produce `MultihashDigest`s. Details about these can be found in the [multiformats multihash interface definitions](https://github.com/multiformats/js-multiformats/blob/master/src/hashes/interface.ts).
46

57
```js
6-
import multiformats from 'multiformats/basics'
7-
import sha3 from '@multiformats/sha3'
8+
import * as Block from 'multiformats/block'
9+
import * as codec from '@ipld/dag-cbor'
10+
import { sha3256 as hasher } from '@multiformats/sha3'
11+
12+
async function run () {
13+
const value = { hello: 'world' }
14+
const block = await Block.encode({ value, hasher, codec })
15+
console.log(block.cid)
16+
// -> CID(bafyrmidyqnbqbeh5lmkwavjizfmsz6ezwvjleweh5frwk56akfyugoio2e)
17+
}
18+
19+
run().catch(console.error)
20+
```
21+
22+
## Usage
23+
24+
The `@multiformats/sha3` package exports `sha3*`, `shake*` and `keccak*` `MultihashHasher`s. The Multicodecs [table](https://github.com/multiformats/multicodec/blob/master/table.csv) defines these multihashes.
25+
26+
The following `MultihashHasher`s are exported:
827

9-
const { multihash } = multiformats
10-
multihash.add(sha3)
28+
* `sha3224` - SHA3-224
29+
* `sha3256` - SHA3-256
30+
* `sha3384` - SHA3-384
31+
* `sha3512` - SHA3-512
32+
* `shake128` - SHAKE-128 (256 output bits)
33+
* `shake256` - SHAKE-256 (512 output bits)
34+
* `keccak224` - KECCAK-224
35+
* `keccak256` - KECCAK-256
36+
* `keccak384` - KECCAK-384
37+
* `keccak512` - KECCAK-512
1138

12-
const data = new Uint8Array([...someData])
13-
const hash = await multihash.hash(data, 'sha3-384')
39+
40+
e.g. he `sha3-384`, multicodec code `0x15`, may be imported as:
41+
42+
```js
43+
import { sha3384 } from '@multiformats/sha3'
1444
```
1545

16-
This package contains hash functions for `sha3-224`, `sha3-256`, `sha3-384`,`sha3-512`, `shake-128`, `shake-256`, `keccak-224`, `keccak-256`, `keccak-384`, and `keccak-512`.
46+
## License
47+
48+
Licensed under either of
49+
50+
* Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / http://www.apache.org/licenses/LICENSE-2.0)
51+
* MIT ([LICENSE-MIT](LICENSE-MIT) / http://opensource.org/licenses/MIT)
52+
53+
### Contribution
54+
55+
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

example.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as Block from 'multiformats/block'
2+
import * as codec from '@ipld/dag-cbor'
3+
import { sha3256 as hasher } from '@multiformats/sha3'
4+
5+
async function run () {
6+
const value = { hello: 'world' }
7+
const block = await Block.encode({ value, hasher, codec })
8+
console.log(block.cid)
9+
// -> CID(bafyrmidyqnbqbeh5lmkwavjizfmsz6ezwvjleweh5frwk56akfyugoio2e)
10+
}
11+
12+
run().catch(console.error)

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ function encoder (fn) {
1111
return (/** @type {Uint8Array} */ b) => new Uint8Array(fn.array(b))
1212
}
1313

14-
export const sha3512 = from({ code: 0x14, name: 'sha3-512', encode: encoder(sha3.sha3_512) })
15-
export const sha3384 = from({ code: 0x15, name: 'sha3-384', encode: encoder(sha3.sha3_384) })
16-
export const sha3256 = from({ code: 0x16, name: 'sha3-256', encode: encoder(sha3.sha3_256) })
1714
export const sha3224 = from({ code: 0x17, name: 'sha3-224', encode: encoder(sha3.sha3_224) })
15+
export const sha3256 = from({ code: 0x16, name: 'sha3-256', encode: encoder(sha3.sha3_256) })
16+
export const sha3384 = from({ code: 0x15, name: 'sha3-384', encode: encoder(sha3.sha3_384) })
17+
export const sha3512 = from({ code: 0x14, name: 'sha3-512', encode: encoder(sha3.sha3_512) })
1818
export const shake128 = from({ code: 0x18, name: 'shake-128', encode: (b) => new Uint8Array(sha3.shake128.array(b, 256)) })
1919
export const shake256 = from({ code: 0x19, name: 'shake-256', encode: (b) => new Uint8Array(sha3.shake256.array(b, 512)) })
2020
export const keccak224 = from({ code: 0x1a, name: 'keccak-224', encode: encoder(sha3.keccak224) })

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"multiformats": "^9.4.1"
3636
},
3737
"devDependencies": {
38+
"@ipld/dag-cbor": "^6.0.5",
3839
"c8": "^7.7.3",
3940
"chai": "^4.3.4",
4041
"ipjs": "^5.0.2",

0 commit comments

Comments
 (0)