编辑期间对HMAC API的不确定引用
我在下面附上了一个代码段,该代码段是使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论