我有以下代码:
void Impl::sign()
{
assert(!canonicalMessage_.empty());
char* key = b64Decode(secureKey_);
EVP_PKEY* pKey = NULL;
EVP_MD_CTX* mdctx = NULL;
std::size_t* slen = NULL;
unsigned char** sig = NULL;
*sig = NULL;
// Create the Message Digest Context
mdctx = EVP_MD_CTX_create();
// Initialize the DigestSign operation.
EVP_DigestSignInit(mdctx, NULL, EVP_sha256(), NULL, pKey);
// Call update with the message
const char* msg = canonicalMessage_.c_str();
EVP_DigestSignUpdate(mdctx, msg, strlen(msg));
// Obtain the length of the signature.
EVP_DigestSignFinal(mdctx, NULL, slen);
// Allocate memory for the signature based on size in slen
*sig = (unsigned char*)OPENSSL_malloc(sizeof(unsigned char) * (*slen));
// Obtain the signature
EVP_DigestSignFinal(mdctx, *sig, slen);
/* Clean up */
if (*sig) OPENSSL_free(*sig);
if(mdctx) EVP_MD_CTX_destroy(mdctx);
}
这里我使用b64Decode()
内部函数获取解码密钥(定义无关紧要)。我的问题是如何将pKey
( 由key
初始化)传递给EVP_DigestSignInit
函数。我找到了与此相关的链接(openSSL: how to initialize keys for public key encryption?),但与我的情况不同,此处使用了文件。
答案 0 :(得分:1)
实际上,我通过下载此处指定的源代码找到了解决方案:https://wiki.openssl.org/index.php/EVP_Signing_and_Verifying
解决方案:
Dir/B/S/A-D-L-S