From 2110af0c65101495932482c5e77310dfc209da62 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Wed, 1 Sep 2021 02:03:47 -0700 Subject: [PATCH] Add support for get/set of a partial hash (#16) * Add support for get/set of a partial hash * make actually mergable --- README.md | 8 ++++++++ index.js | 8 ++++++++ test.js | 16 ++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/README.md b/README.md index 2eb4f1b..7b42b13 100644 --- a/README.md +++ b/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. diff --git a/index.js b/index.js index 8409eb5..4c61835 100644 --- a/index.js +++ b/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) { diff --git a/test.js b/test.js index cc90b8f..ed95aa8 100644 --- a/test.js +++ b/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')