更新openssl,不要工作hmac

时间:2017-12-06 19:24:48

标签: c++ openssl hmac

有代码

 #include <stdio.h>
#include <string.h>
#include <openssl/hmac.h>

int main() {
    const char key[] = "012345678";
    char data[] = "hello world";
    unsigned char* result;
    unsigned int len = 20;
    result = (unsigned char*)malloc(sizeof(char) * len);
    HMAC_CTX ctx;
    HMAC_CTX_init(&ctx);
    HMAC_Init_ex(&ctx, key, strlen(key), EVP_sha1(), NULL);
    HMAC_Update(&ctx, (unsigned char*)&data, strlen(data));
    HMAC_Final(&ctx, result, &len);
    HMAC_CTX_cleanup(&ctx);
    printf("HMAC digest: ");

    for (int i = 0; i != len; i++)
        printf("%02x", (unsigned int)result[i]);

    printf("\n");
    free(result);

}

输出HMAC摘要:e19e220122b37b708bfb95aca2577905acabf0c0

我无法为openssl 1.1.0更新此代码。 新版本更改了语法hmac。

#include <iostream>
#include <string.h>
#include <openssl/hmac.h>


using namespace std;
int main() {
    const char key[] = "012345678";
    char data[] = "hello world";
    int datalen = strlen(data);
    int keylen = strlen(key);


    unsigned char* result;
    unsigned int len = 20;
    result = (unsigned char*)malloc(sizeof(char) * len);
    cout << datalen  << endl;
    cout << keylen  << endl;
    cout << len  << endl;

    HMAC_CTX *HMAC_CTX_new();
    int HMAC_CTX_reset(HMAC_CTX *ctx);
    int HMAC_Init_ex(HMAC_CTX *ctx, const  char key, int keylen, const EVP_MD  EVP_sha1());
    int HMAC_Update(HMAC_CTX * ctx,  char &data, int datalen);
    int HMAC_Final(HMAC_CTX * ctx, char result, int &len);
    int HMAC_CTX_cleanup(HMAC_CTX * ctx);

    printf("HMAC digest: ");

    for (int i = 0; i != len; i++)
        printf("%02x", (unsigned int)result[i]);

    printf("\n");
    free(result);
    return 0;
}

输出HMAC摘要:0000000000000000000000000000000000000000

我会很高兴得到任何帮助

0 个答案:

没有答案