一.MD5对字符串加密
我们使用openssl库对字符串进行MD5加密
#include <openssl/md5.h>
#include <openssl/evp.h>std::string calculate_md5(const std::string &input)
{unsigned char digest[MD5_DIGEST_LENGTH];std::string md5;EVP_MD_CTX *mdContext = EVP_MD_CTX_new();EVP_DigestInit(mdContext, EVP_md5());EVP_DigestUpdate(mdContext, input.c_str(), input.length());EVP_DigestFinal(mdContext, digest, nullptr);EVP_MD_CTX_free(mdContext);char hexDigest[MD5_DIGEST_LENGTH * 2 + 1];for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) {sprintf(hexDigest + (i * 2), "%02x", digest[i]);}hexDigest[MD5_DIGEST_LENGTH * 2] = '\0';md5 = std::string(hexDigest);return md5;
}
二.利用boost获取文件MD5值
bool get_md5(std::string& filename, std::string& md5_out)
{unsigned char digest[16];MD5_CTX