?C++?,???????????MD5???SHA????????openssl
???MD5?SHA-256???????:
#include#include #include #include std::string md5(std::string input) { unsigned char digest[MD5_DIGEST_LENGTH]; MD5((unsigned char*)input.c_str(), input.length(), digest); char mdString[(MD5_DIGEST_LENGTH * 2) + 1]; for(int i = 0; i < MD5_DIGEST_LENGTH; i++) sprintf(&mdString[i*2], "x", (unsigned int)digest[i]); return std::string(mdString); } std::string sha256(std::string input) { unsigned char digest[SHA256_DIGEST_LENGTH]; SHA256((unsigned const char*)input.c_str(), input.length(), digest); char mdString[(SHA256_DIGEST_LENGTH * 2) + 1]; for(int i = 0; i < SHA256_DIGEST_LENGTH; i++) sprintf(&mdString[i*2], "x", (unsigned int)digest[i]); return std::string(mdString); } int main() { std::string input = "Hello, world!"; std::string md5hash = md5(input); std::string sha256hash = sha256(input); std::cout << "MD5 hash: " << md5hash << std::endl; std::cout << "SHA-256 hash: " << sha256hash << std::endl; return 0; }
???????,md5
???sha256
??????????????MD5?SHA-256????main
??????????????????????
??????,MD5??????????,??????????,???????????SHA-256??????????????????????????????,???????SHA????,?SHA-384?SHA-512?