【问题标题】:openSSL: How to initialize EVP_PKEY object with the decoded key?openSSL:如何使用解码的密钥初始化 EVP_PKEY 对象?
【发布时间】:2017-11-03 11:17:54
【问题描述】:

我有以下代码:

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() 内部函数获取解码密钥(定义无关紧要)。我的问题是如何将pKeykey初始化)传递给EVP_DigestSignInit函数。我找到了与此相关的链接 (openSSL: how to initialize keys for public key encryption?),但与我的情况不同,这里使用了文件。

【问题讨论】:

    标签: c++ c openssl


    【解决方案1】:

    其实我通过下载这里指定的源码找到了解决办法:https://wiki.openssl.org/index.php/EVP_Signing_and_Verifying

    解决方案:

    unsigned char* key = b64Decode(secureKey_);
    const EVP_MD* md = EVP_get_digestbyname("sha256");
    EVP_PKEY* pKey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, key, EVP_MD_size(md));
    

    【讨论】:

      猜你喜欢
      • 2012-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-28
      • 1970-01-01
      相关资源
      最近更新 更多