Skip to content

Commit 135fca5

Browse files
Renegade334aduh95
authored andcommitted
crypto: avoid copying buffers to UTF-8 strings in crypto.hash()
PR-URL: #59067 Fixes: #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>
1 parent 324d9fc commit 135fca5

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

src/crypto/crypto_hash.cc

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -274,26 +274,23 @@ void Hash::OneShotDigest(const FunctionCallbackInfo<Value>& args) {
274274
}
275275

276276
DataPointer output = ([&]() -> DataPointer {
277-
Utf8Value utf8(isolate, args[3]);
278-
ncrypto::Buffer<const unsigned char> buf;
279277
if (args[3]->IsString()) {
280-
buf = {
278+
Utf8Value utf8(isolate, args[3]);
279+
ncrypto::Buffer<const unsigned char> buf = {
281280
.data = reinterpret_cast<const unsigned char*>(utf8.out()),
282281
.len = utf8.length(),
283282
};
284-
} else {
285-
ArrayBufferViewContents<unsigned char> input(args[3]);
286-
buf = {
287-
.data = reinterpret_cast<const unsigned char*>(input.data()),
288-
.len = input.length(),
289-
};
290-
}
291-
292-
if (is_xof) {
293-
return ncrypto::xofHashDigest(buf, md, output_length);
283+
return is_xof ? ncrypto::xofHashDigest(buf, md, output_length)
284+
: ncrypto::hashDigest(buf, md);
294285
}
295286

296-
return ncrypto::hashDigest(buf, md);
287+
ArrayBufferViewContents<unsigned char> input(args[3]);
288+
ncrypto::Buffer<const unsigned char> buf = {
289+
.data = reinterpret_cast<const unsigned char*>(input.data()),
290+
.len = input.length(),
291+
};
292+
return is_xof ? ncrypto::xofHashDigest(buf, md, output_length)
293+
: ncrypto::hashDigest(buf, md);
297294
})();
298295

299296
if (!output) [[unlikely]] {

0 commit comments

Comments
 (0)