diff options
author | Ben Noordhuis <info@bnoordhuis.nl> | 2020-02-13 14:05:18 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2020-03-11 21:30:56 +0300 |
commit | 3b9a4035e7fdeeecd35224eaef3f7e67e4609973 (patch) | |
tree | c79bc0f2b28afeb547d58b6a8f7645c3e5f1881e /src/node_crypto.cc | |
parent | d09e1da669f5cc3229f31167a5849566b0de0400 (diff) |
crypto: optimize sign.update() and verify.update()
Use `StringBytes::InlineDecoder` to decode strings inputs in C++ land
instead of decoding them to buffers in JS land before passing them on
to the C++ layer. This is what the other update() methods already did.
PR-URL: https://github.com/nodejs/node/pull/31767
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Diffstat (limited to 'src/node_crypto.cc')
-rw-r--r-- | src/node_crypto.cc | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 1480eb04f88..3a1ff9de476 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -4509,12 +4509,25 @@ void Sign::SignInit(const FunctionCallbackInfo<Value>& args) { void Sign::SignUpdate(const FunctionCallbackInfo<Value>& args) { + Environment* env = Environment::GetCurrent(args); + Sign* sign; ASSIGN_OR_RETURN_UNWRAP(&sign, args.Holder()); Error err; - ArrayBufferViewContents<char> buf(args[0]); - err = sign->Update(buf.data(), buf.length()); + + // Only copy the data if we have to, because it's a string + if (args[0]->IsString()) { + StringBytes::InlineDecoder decoder; + enum encoding enc = ParseEncoding(env->isolate(), args[1], UTF8); + + if (decoder.Decode(env, args[0].As<String>(), enc).IsNothing()) + return; + err = sign->Update(decoder.out(), decoder.size()); + } else { + ArrayBufferViewContents<char> buf(args[0]); + err = sign->Update(buf.data(), buf.length()); + } sign->CheckThrow(err); } @@ -4834,12 +4847,25 @@ void Verify::VerifyInit(const FunctionCallbackInfo<Value>& args) { void Verify::VerifyUpdate(const FunctionCallbackInfo<Value>& args) { + Environment* env = Environment::GetCurrent(args); + Verify* verify; ASSIGN_OR_RETURN_UNWRAP(&verify, args.Holder()); Error err; - ArrayBufferViewContents<char> buf(args[0]); - err = verify->Update(buf.data(), buf.length()); + + // Only copy the data if we have to, because it's a string + if (args[0]->IsString()) { + StringBytes::InlineDecoder decoder; + enum encoding enc = ParseEncoding(env->isolate(), args[1], UTF8); + + if (decoder.Decode(env, args[0].As<String>(), enc).IsNothing()) + return; + err = verify->Update(decoder.out(), decoder.size()); + } else { + ArrayBufferViewContents<char> buf(args[0]); + err = verify->Update(buf.data(), buf.length()); + } verify->CheckThrow(err); } |