◐ Shell
clean mode source ↗

crypto: avoid copying buffers to UTF-8 strings in `crypto.hash()` · nodejs/node@135fca5

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]] {