Skip to content

Commit fc4a8af

Browse files
Renegade334panva
authored andcommitted
crypto: avoid copying buffers to UTF-8 strings in crypto.hash()
PR-URL: #59067 Fixes: #59057 Reviewed-By: Filip Skokan <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 0fd1ecd commit fc4a8af

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
@@ -275,26 +275,23 @@ void Hash::OneShotDigest(const FunctionCallbackInfo<Value>& args) {
275275
}
276276

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

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

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

0 commit comments

Comments
 (0)