8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ce686c0 commit e83671cCopy full SHA for e83671c
src/node_crypto.cc
@@ -145,6 +145,26 @@ template int SSLWrap<TLSWrap>::SelectALPNCallback(
145
unsigned int inlen,
146
void* arg);
147
148
+template <typename T>
149
+void Decode(const FunctionCallbackInfo<Value>& args,
150
+ void (*callback)(T*, const FunctionCallbackInfo<Value>&,
151
+ const char*, size_t)) {
152
+ T* ctx;
153
+ ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
154
+
155
+ if (args[0]->IsString()) {
156
+ StringBytes::InlineDecoder decoder;
157
+ Environment* env = Environment::GetCurrent(args);
158
+ enum encoding enc = ParseEncoding(env->isolate(), args[1], UTF8);
159
+ if (decoder.Decode(env, args[0].As<String>(), enc).IsNothing())
160
+ return;
161
+ callback(ctx, args, decoder.out(), decoder.size());
162
+ } else {
163
+ ArrayBufferViewContents<char> buf(args[0]);
164
+ callback(ctx, args, buf.data(), buf.length());
165
+ }
166
+}
167
168
static int PasswordCallback(char* buf, int size, int rwflag, void* u) {
169
const char* passphrase = static_cast<char*>(u);
170
if (passphrase != nullptr) {
@@ -3946,38 +3966,24 @@ CipherBase::UpdateResult CipherBase::Update(const char* data,
3946
3966
3947
3967
3948
3968
void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
3949
- Environment* env = Environment::GetCurrent(args);
3950
-
3951
- CipherBase* cipher;
3952
- ASSIGN_OR_RETURN_UNWRAP(&cipher, args.Holder());
3953
3954
- AllocatedBuffer out;
3955
- UpdateResult r;
3956
3957
- // Only copy the data if we have to, because it's a string
3958
- if (args[0]->IsString()) {
3959
- StringBytes::InlineDecoder decoder;
3960
- enum encoding enc = ParseEncoding(env->isolate(), args[1], UTF8);
3961
3962
- if (decoder.Decode(env, args[0].As<String>(), enc).IsNothing())
3969
+ Decode<CipherBase>(args, [](CipherBase* cipher,
3970
+ const FunctionCallbackInfo<Value>& args,
3971
+ const char* data, size_t size) {
3972
+ AllocatedBuffer out;
3973
+ UpdateResult r = cipher->Update(data, size, &out);
3974
3975
+ if (r != kSuccess) {
3976
+ if (r == kErrorState) {
3977
3978
+ ThrowCryptoError(env, ERR_get_error(),
3979
+ "Trying to add data in unsupported state");
3980
3963
3981
return;
3964
- r = cipher->Update(decoder.out(), decoder.size(), &out);
3965
- } else {
- ArrayBufferViewContents<char> buf(args[0]);
- r = cipher->Update(buf.data(), buf.length(), &out);
- }
- if (r != kSuccess) {
- if (r == kErrorState) {
- ThrowCryptoError(env, ERR_get_error(),
- "Trying to add data in unsupported state");
3982
}
- return;
- CHECK(out.data() != nullptr || out.size() == 0);
3983
- args.GetReturnValue().Set(out.ToBuffer().ToLocalChecked());
3984
+ CHECK(out.data() != nullptr || out.size() == 0);
3985
+ args.GetReturnValue().Set(out.ToBuffer().ToLocalChecked());
3986
+ });
3987
3988
3989
@@ -4139,26 +4145,11 @@ bool Hmac::HmacUpdate(const char* data, int len) {
4139
4145
4140
4146
4141
4147
void Hmac::HmacUpdate(const FunctionCallbackInfo<Value>& args) {
4142
4143
4144
- Hmac* hmac;
- ASSIGN_OR_RETURN_UNWRAP(&hmac, args.Holder());
4148
- bool r = false;
4149
4150
4151
4152
4153
- if (!decoder.Decode(env, args[0].As<String>(), enc).IsNothing()) {
4154
- r = hmac->HmacUpdate(decoder.out(), decoder.size());
4155
4156
4157
4158
- r = hmac->HmacUpdate(buf.data(), buf.length());
4159
4160
4161
- args.GetReturnValue().Set(r);
+ Decode<Hmac>(args, [](Hmac* hmac, const FunctionCallbackInfo<Value>& args,
+ bool r = hmac->HmacUpdate(data, size);
+ args.GetReturnValue().Set(r);
4162
4163
4164
@@ -4287,28 +4278,11 @@ bool Hash::HashUpdate(const char* data, int len) {
4287
4278
4288
4279
4289
4280
void Hash::HashUpdate(const FunctionCallbackInfo<Value>& args) {
4290
4291
4292
- Hash* hash;
4293
- ASSIGN_OR_RETURN_UNWRAP(&hash, args.Holder());
4294
4295
4296
- bool r = true;
4297
4298
4299
4300
4301
- if (decoder.Decode(env, args[0].As<String>(), enc).IsNothing()) {
4302
- args.GetReturnValue().Set(false);
4303
4304
4305
- r = hash->HashUpdate(decoder.out(), decoder.size());
4306
- } else if (args[0]->IsArrayBufferView()) {
4307
- ArrayBufferViewContents<char> buf(args[0].As<ArrayBufferView>());
4308
- r = hash->HashUpdate(buf.data(), buf.length());
4309
4310
4311
4281
+ Decode<Hash>(args, [](Hash* hash, const FunctionCallbackInfo<Value>& args,
4282
4283
+ bool r = hash->HashUpdate(data, size);
4284
4285
4312
4286
4313
4314
@@ -4509,27 +4483,11 @@ void Sign::SignInit(const FunctionCallbackInfo<Value>& args) {
4509
4483
4510
4484
4511
4485
void Sign::SignUpdate(const FunctionCallbackInfo<Value>& args) {
4512
4513
4514
- Sign* sign;
4515
- ASSIGN_OR_RETURN_UNWRAP(&sign, args.Holder());
4516
4517
- Error err;
4518
4519
4520
4521
4522
4523
4524
4525
4526
- err = sign->Update(decoder.out(), decoder.size());
4527
4528
4529
- err = sign->Update(buf.data(), buf.length());
4530
4531
4532
- sign->CheckThrow(err);
4486
+ Decode<Sign>(args, [](Sign* sign, const FunctionCallbackInfo<Value>& args,
4487
4488
+ Error err = sign->Update(data, size);
4489
+ sign->CheckThrow(err);
4490
4533
4491
4534
4492
4535
4493
static int GetDefaultSignPadding(const ManagedEVPPKey& key) {
@@ -4847,27 +4805,12 @@ void Verify::VerifyInit(const FunctionCallbackInfo<Value>& args) {
4847
4805
4848
4806
4849
4807
void Verify::VerifyUpdate(const FunctionCallbackInfo<Value>& args) {
4850
4851
4852
- Verify* verify;
4853
- ASSIGN_OR_RETURN_UNWRAP(&verify, args.Holder());
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
- err = verify->Update(decoder.out(), decoder.size());
4865
4866
4867
- err = verify->Update(buf.data(), buf.length());
4868
4869
4870
- verify->CheckThrow(err);
4808
+ Decode<Verify>(args, [](Verify* verify,
4809
4810
4811
+ Error err = verify->Update(data, size);
4812
+ verify->CheckThrow(err);
4813
4871
4814
4872
4815
4873
4816