C语言计算MD5值
时间: 2023-08-01 16:13:09 浏览: 55
要计算字符串的 MD5 值,可以使用 OpenSSL 库提供的函数。以下是一个简单的示例程序:
```c
#include <stdio.h>
#include <string.h>
#include <openssl/md5.h>
int main() {
char str[] = "Hello, world!";
unsigned char digest[MD5_DIGEST_LENGTH];
MD5_CTX context;
MD5_Init(&context);
MD5_Update(&context, str, strlen(str));
MD5_Final(digest, &context);
printf("MD5 value: ");
for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
printf("%02x", digest[i]);
}
printf("\n");
return 0;
}
```
该程序使用了 OpenSSL 库中的 MD5 函数库,并将字符串 "Hello, world!" 的 MD5 值计算出来。需要在编译时链接 libcrypto 库,例如:
```shell
gcc -o md5 md5.c -lcrypto
```
运行程序后,将输出以下内容:
```
MD5 value: b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
```
这是 "Hello, world!" 的 MD5 值。