crypto: support outputLength option in crypto.hash for XOF functions

Support `outputLength` option in crypto.hash() for XOF hash
functions to align with the behaviour of crypto.createHash()
API

closes: https://github.com/nodejs/node/issues/57312

Co-authored-by: Filip Skokan <panva.ip@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/58121
Fixes: https://github.com/nodejs/node/issues/57312
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
This commit is contained in:
Aditi 2025-07-08 19:09:00 +05:30 committed by GitHub
parent c7eff619c2
commit 1c4fe6d795
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 258 additions and 23 deletions

View file

@ -4191,6 +4191,22 @@ DataPointer hashDigest(const Buffer<const unsigned char>& buf,
return data.resize(result_size);
}
DataPointer xofHashDigest(const Buffer<const unsigned char>& buf,
const EVP_MD* md,
size_t output_length) {
if (md == nullptr) return {};
EVPMDCtxPointer ctx = EVPMDCtxPointer::New();
if (!ctx) return {};
if (ctx.digestInit(md) != 1) {
return {};
}
if (ctx.digestUpdate(reinterpret_cast<const Buffer<const void>&>(buf)) != 1) {
return {};
}
return ctx.digestFinal(output_length);
}
// ============================================================================
X509Name::X509Name() : name_(nullptr), total_(0) {}

View file

@ -278,8 +278,13 @@ class Digest final {
const EVP_MD* md_ = nullptr;
};
// Computes a fixed-length digest.
DataPointer hashDigest(const Buffer<const unsigned char>& data,
const EVP_MD* md);
// Computes a variable-length digest for XOF algorithms (e.g. SHAKE128).
DataPointer xofHashDigest(const Buffer<const unsigned char>& data,
const EVP_MD* md,
size_t length);
class Cipher final {
public: