Skip to content

Commit 7690da9

Browse files
Aditi-1400panva
authored andcommitted
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: nodejs/node#57312 Co-authored-by: Filip Skokan <[email protected]> PR-URL: nodejs/node#58121 Fixes: nodejs/node#57312 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Filip Skokan <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]>
1 parent 621d187 commit 7690da9

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

include/ncrypto.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,13 @@ class Digest final {
284284
const EVP_MD* md_ = nullptr;
285285
};
286286

287+
// Computes a fixed-length digest.
287288
DataPointer hashDigest(const Buffer<const unsigned char>& data,
288289
const EVP_MD* md);
290+
// Computes a variable-length digest for XOF algorithms (e.g. SHAKE128).
291+
DataPointer xofHashDigest(const Buffer<const unsigned char>& data,
292+
const EVP_MD* md,
293+
size_t length);
289294

290295
class Cipher final {
291296
public:

src/ncrypto.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4384,6 +4384,22 @@ DataPointer hashDigest(const Buffer<const unsigned char>& buf,
43844384
return data.resize(result_size);
43854385
}
43864386

4387+
DataPointer xofHashDigest(const Buffer<const unsigned char>& buf,
4388+
const EVP_MD* md,
4389+
size_t output_length) {
4390+
if (md == nullptr) return {};
4391+
4392+
EVPMDCtxPointer ctx = EVPMDCtxPointer::New();
4393+
if (!ctx) return {};
4394+
if (ctx.digestInit(md) != 1) {
4395+
return {};
4396+
}
4397+
if (ctx.digestUpdate(reinterpret_cast<const Buffer<const void>&>(buf)) != 1) {
4398+
return {};
4399+
}
4400+
return ctx.digestFinal(output_length);
4401+
}
4402+
43874403
// ============================================================================
43884404

43894405
X509Name::X509Name() : name_(nullptr), total_(0) {}

0 commit comments

Comments
 (0)