From 2a23cf295523c5f8fa94e833c2cfd9f822b77920 Mon Sep 17 00:00:00 2001 From: Renegade334 Date: Mon, 14 Jul 2025 08:04:52 +0100 Subject: [PATCH] crypto: avoid copying buffers to UTF-8 strings in `crypto.hash()` --- src/crypto/crypto_hash.cc | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/crypto/crypto_hash.cc b/src/crypto/crypto_hash.cc index d56cedf626999b..f05d216f3429a3 100644 --- a/src/crypto/crypto_hash.cc +++ b/src/crypto/crypto_hash.cc @@ -275,26 +275,23 @@ void Hash::OneShotDigest(const FunctionCallbackInfo& args) { } DataPointer output = ([&]() -> DataPointer { - Utf8Value utf8(isolate, args[3]); - ncrypto::Buffer buf; if (args[3]->IsString()) { - buf = { + Utf8Value utf8(isolate, args[3]); + ncrypto::Buffer buf = { .data = reinterpret_cast(utf8.out()), .len = utf8.length(), }; - } else { - ArrayBufferViewContents input(args[3]); - buf = { - .data = reinterpret_cast(input.data()), - .len = input.length(), - }; - } - - if (is_xof) { - return ncrypto::xofHashDigest(buf, md, output_length); + return is_xof ? ncrypto::xofHashDigest(buf, md, output_length) + : ncrypto::hashDigest(buf, md); } - return ncrypto::hashDigest(buf, md); + ArrayBufferViewContents input(args[3]); + ncrypto::Buffer buf = { + .data = reinterpret_cast(input.data()), + .len = input.length(), + }; + return is_xof ? ncrypto::xofHashDigest(buf, md, output_length) + : ncrypto::hashDigest(buf, md); })(); if (!output) [[unlikely]] {