From bb039ef5fb7ed7db2f1ac28ea469ceea08d90a6b Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Thu, 10 Oct 2019 00:33:15 +0200 Subject: [PATCH 1/2] crypto: add Hash.prototype.copy() method Make it possible to clone the internal state of a Hash object into a new Hash object, i.e., to fork the state of the object. Fixes: https://github.com/nodejs/node/issues/29903 --- doc/api/crypto.md | 20 ++++++++++++++++++++ lib/internal/crypto/hash.js | 11 ++++++++++- src/node_crypto.cc | 23 +++++++++++++++++------ src/node_crypto.h | 2 +- test/parallel/test-crypto-hash.js | 29 +++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+), 8 deletions(-) diff --git a/doc/api/crypto.md b/doc/api/crypto.md index b4f91c1d559e71..fe5ddad29367ac 100644 --- a/doc/api/crypto.md +++ b/doc/api/crypto.md @@ -1041,6 +1041,26 @@ console.log(hash.digest('hex')); // 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50 ``` +### hash.copy(\[options\]) + + +* `options` {Object} [`stream.transform` options][] +* Returns: {Hash} + +Creates a new `Hash` object that contains a deep copy of the internal state +of the current `Hash` object. + +The optional `options` argument controls stream behavior. For XOF hash +functions such as `'shake256'`, the `outputLength` option can be used to +specify the desired output length in bytes. + +An error is thrown when an attempt is made to copy the `Hash` object after +its [`hash.digest()`][] method has been called. + ### hash.digest(\[encoding\])