Browse Source

Add support for get/set of a partial hash (#16)

* Add support for get/set of a partial hash

* make actually mergable
master
Blaine Bublitz 3 years ago
committed by GitHub
parent
commit
2110af0c65
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      README.md
  2. 8
      index.js
  3. 16
      test.js

8
README.md

@ -44,6 +44,14 @@ Update the hash with a new piece of data. `data` should be a buffer or uint8arra
Digest the hash.
#### `hash.getPartialHash()`
Returns the current partial hash.
#### `hash.setPartialHash(data)`
Set the hash to a previously set hash. `data` should be the result of `getPartialHash()` (which returns uint8array)
#### `var promise = blake2b.ready([cb])`
Wait for the WASM code to load. Returns the WebAssembly instance promise as well for convenience.

8
index.js

@ -123,6 +123,14 @@ Blake2b.ready = function (cb) {
Blake2b.prototype.ready = Blake2b.ready
Blake2b.prototype.getPartialHash = function () {
return wasm.memory.slice(this.pointer, this.pointer + 216);
}
Blake2b.prototype.setPartialHash = function (ph) {
wasm.memory.set(ph, this.pointer);
}
function noop () {}
function hexSlice (buf, start, len) {

16
test.js

@ -55,6 +55,22 @@ blake2b.ready(function () {
t.end()
})
tape('allows getting & setting a partial hash', function (t) {
var a = blake2b(64)
var b = blake2b(64)
var partialHash = a
.update(Buffer.from('hello'))
.update(Buffer.from(' '))
.update(Buffer.from('world'))
.getPartialHash()
b.setPartialHash(partialHash)
t.same(a.digest(), b.digest())
t.end()
})
vectors.forEach(function (vector, i) {
tape('test-vectors.json #' + i, function (t) {
var key = vector.key && Buffer.from(vector.key, 'hex')

Loading…
Cancel
Save