mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
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>
This commit is contained in:
1 parent
025f658fa6
commit
ce686c03ae
2 files changed
+40
-7
No files matched your search
@@ -9,7 +9,7 @@ const {
|
||||
ERR_INVALID_ARG_TYPE,
|
||||
ERR_INVALID_OPT_VALUE
|
||||
} = require('internal/errors').codes;
|
||||
const { validateString } = require('internal/validators');
|
||||
const { validateEncoding, validateString } = require('internal/validators');
|
||||
const {
|
||||
Sign: _Sign,
|
||||
Verify: _Verify,
|
||||
@@ -50,8 +50,15 @@ Sign.prototype._write = function _write(chunk, encoding, callback) {
|
||||
|
||||
Sign.prototype.update = function update(data, encoding) {
|
||||
encoding = encoding || getDefaultEncoding();
|
||||
data = getArrayBufferView(data, 'data', encoding);
|
||||
this[kHandle].update(data);
|
||||
|
||||
if (typeof data === 'string') {
|
||||
validateEncoding(data, encoding);
|
||||
} else if (!isArrayBufferView(data)) {
|
||||
throw new ERR_INVALID_ARG_TYPE(
|
||||
'data', ['string', 'Buffer', 'TypedArray', 'DataView'], data);
|
||||
}
|
||||
|
||||
this[kHandle].update(data, encoding);
|
||||
return this;
|
||||
};
|
||||
|
||||
|
||||
+30
-4
@@ -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);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user