I am using a openssl EVP_PKEY_sign
where the signature buffer size is already known and can be allocated using a vector.
size_t SignatureLength;
std::vector<unsigned char> Signature;
EVP_PKEY_sign(EvpPkeyCtx, NULL, &SignatureLength, MessageDigest.data(), MessageDigest.size());
Signature.resize(SignatureLength);
EVP_PKEY_sign(EvpPkeyCtx, Signature.data(), &SignatureLength, MessageDigest.data(), MessageDigest.size());
Signature.resize(SignatureLength);
I am using the Signature.data()
to get the raw pointer to the buffer used by the vector.
The first call to EVP_PKEY_sign
gives the maximum length of the output signature buffer.
On calling resize the buffer is filled-up with 0's which cause an additional overhead making it an O(n) operation where only O(1) allocation is needed.
Alternative is to call reserve but it fails as it only allocates memory and post calling EVP_PKEY_sign
again the MessageDigest.size()
would still be zero and resizing it to actual signature length(from the second call) then would overwrite the buffer with default values.
What is the efficient way to do this?