1 | # blake2b-wasm
|
2 |
|
3 | Blake2b implemented in WASM
|
4 |
|
5 | ```
|
6 | npm install blake2b-wasm
|
7 | ```
|
8 |
|
9 | Works in browsers that support WASM and Node.js 8+.
|
10 |
|
11 | ## Usage
|
12 |
|
13 | ``` js
|
14 | var blake2b = require('blake2b-wasm')
|
15 |
|
16 | if (!blake2b.SUPPORTED) {
|
17 | console.log('WebAssembly not supported by your runtime')
|
18 | }
|
19 |
|
20 | blake2b.ready(function (err) {
|
21 | if (err) throw err
|
22 |
|
23 | var hash = blake2b()
|
24 | .update(Buffer.from('hello')) // pass in a buffer or uint8array
|
25 | .update(Buffer.from(' '))
|
26 | .update(Buffer.from('world'))
|
27 | .digest('hex')
|
28 |
|
29 | console.log('Blake2b hash of "hello world" is %s', hash)
|
30 | })
|
31 | ```
|
32 |
|
33 | ## API
|
34 |
|
35 | #### `var hash = blake2b([digestLength], [key], [salt], [personal])`
|
36 |
|
37 | Create a new hash instance. `digestLength` defaults to `32`.
|
38 |
|
39 | #### `hash.update(data)`
|
40 |
|
41 | Update the hash with a new piece of data. `data` should be a buffer or uint8array.
|
42 |
|
43 | #### `var digest = hash.digest([enc])`
|
44 |
|
45 | Digest the hash.
|
46 |
|
47 | #### `var promise = blake2b.ready([cb])`
|
48 |
|
49 | Wait for the WASM code to load. Returns the WebAssembly instance promise as well for convenience.
|
50 | You have to call this at least once before instantiating the hash.
|
51 |
|
52 | ## Browser demo
|
53 |
|
54 | There is a browser example included in [example.html](example.html) and [example.js](example.js).
|
55 |
|
56 | ## Contributing
|
57 |
|
58 | The bulk of this module is implemented in WebAssembly in the [blake2b.wat](blake2b.wat) file.
|
59 | The format of this file is S-Expressions that can be compiled to their binary WASM representation by doing
|
60 |
|
61 | ```
|
62 | # also available as `npm run compile`
|
63 | wast2wasm blake2b.wat -o blake2b.wasm
|
64 | ```
|
65 |
|
66 | If you do not have `wast2wasm` installed follow the instructions here, https://github.com/WebAssembly/wabt
|
67 |
|
68 | ## License
|
69 |
|
70 | MIT
|