编辑期间对HMAC API的不确定引用

发布于 2025-02-13 18:19:15 字数 1493 浏览 0 评论 0原文

我在下面附上了一个代码段,该代码段是使用OpenSSL V1.0.2U HMAC API创建消息身份验证的代码(MAC)的。我在编译代码时遇到了麻烦。

int main() {
        // The secret key for hashing
        const char key[] = "012345678";
        // The data that we're going to hash
        char data[] = "hello world";
        // Be careful of the length of string with the choosen hash engine. SHA1 needed 20 characters.
        // Change the length accordingly with your choosen hash engine.
        unsigned char* result;
        unsigned int len = 20;
        result = (unsigned char*)malloc(sizeof(char) * len);
        HMAC_CTX *ctx;
        //ctx = HMAC_CTX_new();
        HMAC_CTX_init(&ctx);
        // Using sha1 hash engine here.
        // You may use other hash engines. e.g EVP_md5(), EVP_sha224, EVP_sha512, etc
        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_free(&ctx);
        printf("HMAC digest: ");
        for (int i = 0; i != len; i++)
                printf("%02x", (unsigned int)result[i]);
        printf("\n");
        free(result);
        return 0;
}

用于编译的命令:

gcc -I/usr/local/ssl/include, main.c -Wl,-L/usr/local/ssl/lib -lssl -lcrypto

输出:汇编失败了以下错误:

main.c:(.text+0x66): undefined reference to `HMAC_CTX_init'
collect2: error: ld returned 1 exit status

有人可以在这里帮助我吗?

I have attached a code snippet below which is creating a Message Authenticated Code (MAC) using OpenSSL v1.0.2u HMAC APIs. I having trouble compiling the code.

int main() {
        // The secret key for hashing
        const char key[] = "012345678";
        // The data that we're going to hash
        char data[] = "hello world";
        // Be careful of the length of string with the choosen hash engine. SHA1 needed 20 characters.
        // Change the length accordingly with your choosen hash engine.
        unsigned char* result;
        unsigned int len = 20;
        result = (unsigned char*)malloc(sizeof(char) * len);
        HMAC_CTX *ctx;
        //ctx = HMAC_CTX_new();
        HMAC_CTX_init(&ctx);
        // Using sha1 hash engine here.
        // You may use other hash engines. e.g EVP_md5(), EVP_sha224, EVP_sha512, etc
        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_free(&ctx);
        printf("HMAC digest: ");
        for (int i = 0; i != len; i++)
                printf("%02x", (unsigned int)result[i]);
        printf("\n");
        free(result);
        return 0;
}

Command used for compiling:

gcc -I/usr/local/ssl/include, main.c -Wl,-L/usr/local/ssl/lib -lssl -lcrypto

Output: Compilation failed with the following error:

main.c:(.text+0x66): undefined reference to `HMAC_CTX_init'
collect2: error: ld returned 1 exit status

Can somebody help me out here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文