Skip to content
Closed
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
src: reduce allocations in exportPublicKey()
  • Loading branch information
bnoordhuis committed Jul 17, 2017
commit 083ccd201bfd7fa7eaf954046b10152d7d71f146
16 changes: 7 additions & 9 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5835,7 +5835,7 @@ void VerifySpkac(const FunctionCallbackInfo<Value>& args) {
}


const char* ExportPublicKey(const char* data, int len) {
char* ExportPublicKey(const char* data, int len, size_t* size) {
char* buf = nullptr;
EVP_PKEY* pkey = nullptr;
NETSCAPE_SPKI* spki = nullptr;
Expand All @@ -5855,12 +5855,12 @@ const char* ExportPublicKey(const char* data, int len) {
if (PEM_write_bio_PUBKEY(bio, pkey) <= 0)
goto exit;

BIO_write(bio, "\0", 1);
BUF_MEM* ptr;
BIO_get_mem_ptr(bio, &ptr);

buf = new char[ptr->length];
memcpy(buf, ptr->data, ptr->length);
*size = ptr->length;
buf = Malloc(*size);
memcpy(buf, ptr->data, *size);

exit:
if (pkey != nullptr)
Expand Down Expand Up @@ -5891,14 +5891,12 @@ void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
char* data = Buffer::Data(args[0]);
CHECK_NE(data, nullptr);

const char* pkey = ExportPublicKey(data, length);
size_t pkey_size;
char* pkey = ExportPublicKey(data, length, &pkey_size);
if (pkey == nullptr)
return args.GetReturnValue().SetEmptyString();

Local<Value> out = Encode(env->isolate(), pkey, strlen(pkey), BUFFER);

delete[] pkey;

Local<Value> out = Buffer::New(env, pkey, pkey_size).ToLocalChecked();
args.GetReturnValue().Set(out);
}

Expand Down