2018.12.27
c++的md5
-
include <openssl/md5.h>
- ldflags -lssls
std::string encode_md5(std::string str) {
unsigned char md_str[33] = {0};
MD5((const unsigned char *)str.c_str(), str.length(), md_str);
// 哈希后的十六进制串 32字节
char buf[65] = {0};
char tmp[3] = {0};
for (int i = 0; i < 32; i++) {
snprintf(tmp, sizeof(tmp), "%02x", md_str[i]);
strncat(buf, tmp, sizeof(buf));
}
buf[32] = '\0'; // 后面都是0,从32字节截断
// printf("md5:%s\n", buf);
return std::string(buf);
}