| 4744 | |
| 4745 | #ifdef CPPHTTPLIB_OPENSSL_SUPPORT |
| 4746 | inline std::string message_digest(const std::string &s, const EVP_MD *algo) { |
| 4747 | auto context = std::unique_ptr<EVP_MD_CTX, decltype(&EVP_MD_CTX_free)>( |
| 4748 | EVP_MD_CTX_new(), EVP_MD_CTX_free); |
| 4749 | |
| 4750 | unsigned int hash_length = 0; |
| 4751 | unsigned char hash[EVP_MAX_MD_SIZE]; |
| 4752 | |
| 4753 | EVP_DigestInit_ex(context.get(), algo, nullptr); |
| 4754 | EVP_DigestUpdate(context.get(), s.c_str(), s.size()); |
| 4755 | EVP_DigestFinal_ex(context.get(), hash, &hash_length); |
| 4756 | |
| 4757 | std::stringstream ss; |
| 4758 | for (auto i = 0u; i < hash_length; ++i) { |
| 4759 | ss << std::hex << std::setw(2) << std::setfill('0') |
| 4760 | << static_cast<unsigned int>(hash[i]); |
| 4761 | } |
| 4762 | |
| 4763 | return ss.str(); |
| 4764 | } |
| 4765 | |
| 4766 | inline std::string MD5(const std::string &s) { |
| 4767 | return message_digest(s, EVP_md5()); |