linux c++程序使用MD5

发布时间 2023-10-16 17:14:56作者: bitwoods

为避免找到的开源md5算法有坑,一般直接用openssl自带的MD5相关函数实现;
一般系统已默认安装openssl,没装的话直接指令安装

ubuntu
sudo apt-get install libssl-dev
centos
sudo dnf install openssl-devel

示例代码

#include <openssl/md5.h>

unsigned char md5[MD5_DIGEST_LENGTH];
std::string user = "testuser";
MD5(reinterpret_cast<const unsigned char*>(user.c_str()), strlen(user.c_str()), md5);
//do everything you want

编译报错:
undefined reference to `MD5'

处理:
编译选项加上-lcrypto -lssl

gcc -Wall md5.c -o md5 -lcrypto -lssl