Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
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.
  • Loading branch information
bnoordhuis committed Feb 13, 2020
commit 66f632686b8d570c5969e7b293df62f826082904
13 changes: 10 additions & 3 deletions lib/internal/crypto/sig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
};

Expand Down
34 changes: 30 additions & 4 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4992,12 +4992,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);
}
Expand Down Expand Up @@ -5311,12 +5324,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);
}
Expand Down