我无法使用 OpenSSL 获取 md5 哈希。我正在使用以下命令进行构建:
gcc -Wall test_3.c -o test_3 -lcrypto -lssl
但出现以下链接错误:
undefined reference to `MD5Init'
undefined reference to `MD5Update'
undefined reference to `MD5Final'
collect2: ld returned 1 exit status
程序如下:
#include<stdio.h>
#include<string.h>
#include <openssl/hmac.h>
#include <openssl/md5.h>
int main()
{
char digest[17];
char input[] = "asdfljiahfbqhebfjcnajclgfeliuaef";
int length = strlen(input);
MD5_CTX md5;
MD5Init(&md5);
MD5Update(&md5,input, length);
MD5Final(digest,&md5);
printf("digest is %s \n",digest);
return 0;
}
如果你知道问题请告诉我,请帮助我
最佳答案
你犯了一些错误,我已经改正了。我还添加了哈希的十六进制输出。否则它会破坏你的终端。
#include <stdio.h>
#include <string.h>
#include <openssl/hmac.h>
#include <openssl/md5.h>
int main()
{
// use unsigned char
unsigned char digest[16];
char *input = "hek2mgl";
int length = strlen(input);
int i=0;
// don't miss the underscore after MD5
MD5_CTX md5;
MD5_Init(&md5);
while (length > 0) {
if (length > 512) {
MD5_Update(&md5, input, 512);
} else {
MD5_Update(&md5, input, length);
}
length -= 512;
input += 512;
}
MD5_Final(digest, &md5);
printf("digest is: ");
for(i = 0; i < 16; i++) {
printf("%02x", digest[i]);
}
printf("\n");
return 0;
}
输出:
digest is: 1ff8a3b2958ee3340ed88a2b980a8099
关于linux - 无法获取 md5 哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21756900/