crypto: avoid copying buffers to UTF-8 strings in crypto.hash()

PR-URL: https://github.com/nodejs/node/pull/59067
Fixes: https://github.com/nodejs/node/issues/59057
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Renegade334 authored and Antoine du Hamel committed 2025-07-21 10:59:01 +02:00
1 parent 324d9fc9d4
commit 135fca5b72
1 file changed
+11 -14
+11 -14
View File
@@ -274,26 +274,23 @@ void Hash::OneShotDigest(const FunctionCallbackInfo<Value>& args) {
}
DataPointer output = ([&]() -> DataPointer {
Utf8Value utf8(isolate, args[3]);
ncrypto::Buffer<const unsigned char> buf;
if (args[3]->IsString()) {
buf = {
Utf8Value utf8(isolate, args[3]);
ncrypto::Buffer<const unsigned char> buf = {
.data = reinterpret_cast<const unsigned char*>(utf8.out()),
.len = utf8.length(),
};
} else {
ArrayBufferViewContents<unsigned char> input(args[3]);
buf = {
.data = reinterpret_cast<const unsigned char*>(input.data()),
.len = input.length(),
};
return is_xof ? ncrypto::xofHashDigest(buf, md, output_length)
: ncrypto::hashDigest(buf, md);
}
if (is_xof) {
return ncrypto::xofHashDigest(buf, md, output_length);
}
return ncrypto::hashDigest(buf, md);
ArrayBufferViewContents<unsigned char> input(args[3]);
ncrypto::Buffer<const unsigned char> buf = {
.data = reinterpret_cast<const unsigned char*>(input.data()),
.len = input.length(),
};
return is_xof ? ncrypto::xofHashDigest(buf, md, output_length)
: ncrypto::hashDigest(buf, md);
})();
if (!output) [[unlikely]] {