Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions src/crypto/crypto_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -275,26 +275,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(),
};
}

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<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]] {
Expand Down
Loading