Test environment: centos 6.4 x86_64-bit vmwarevm 1g RAM (physical host CPU i7-3770 3.4 GHz)
Test code (using the OpenSSL hash Library ):
#include <iostream>#include <sstream>#include <string>#include <iomanip>#include <ctime>#include <stdio.h>using namespace std;#include <openssl/sha.h>#include <openssl/md5.h>string md5(const string str){ unsigned char hash[MD5_DIGEST_LENGTH]; MD5_CTX md5; MD5_Init(&md5); MD5_Update(&md5, str.c_str(), str.size()); MD5_Final(hash, &md5); stringstream ss; for(int i = 0; i < MD5_DIGEST_LENGTH; i++) { ss << hex << setw(2) << setfill(‘0‘) << (int)hash[i]; } return ss.str();}string sha256(const string str){ unsigned char hash[SHA256_DIGEST_LENGTH]; SHA256_CTX sha256; SHA256_Init(&sha256); SHA256_Update(&sha256, str.c_str(), str.size()); SHA256_Final(hash, &sha256); stringstream ss; for(int i = 0; i < SHA256_DIGEST_LENGTH; i++) //32 { ss << hex << setw(2) << setfill(‘0‘) << (int)hash[i]; } return ss.str();}string sha512(const string str){ unsigned char hash[SHA512_DIGEST_LENGTH]; SHA512_CTX sha512; SHA512_Init(&sha512); SHA512_Update(&sha512, str.c_str(), str.size()); SHA512_Final(hash, &sha512); stringstream ss; for(int i = 0; i < SHA512_DIGEST_LENGTH; i++) //64 { ss << hex << setw(2) << setfill(‘0‘) << (int)hash[i]; } return ss.str();}int main(){ //MD5 time_t t1=time(0); cout << "MD5:" << endl; cout << t1 << endl; for(int i=0;i<10000000;i++) { char pwd[32]; sprintf(pwd, "%d", i); md5(pwd); } time_t t2=time(0); cout << t2 << endl; cout << t2-t1 <<endl; //SHA256 t1=time(0); cout << "SHA256:" << endl; cout << t1 << endl; for(int i=0;i<10000000;i++) { char pwd[32]; sprintf(pwd, "%d", i); sha256(pwd); } t2=time(0); cout << t2 << endl; cout << t2-t1 <<endl; //SHA512 t1=time(0); cout << "SHA512:" << endl; cout << t1 << endl; for(int i=0;i<10000000;i++) { char pwd[32]; sprintf(pwd, "%d", i); sha512(pwd); } t2=time(0); cout << t2 << endl; cout << t2-t1 <<endl; return 0;}
Compile: G ++-O Sha. cpp-lssl-lcrypto
Run:./Sha
Result:
MD5:1409193206140919322822SHA256:1409193228140919326335SHA512:1409193263140919331855
10 million hash operations were performed respectively. MD5 took 22 seconds, sha256 took 35 seconds, and sha512 took 55 seconds.
The operation time is basically at the same magnitude. It is necessary to use sha256 or sha512 instead of MD5.
The results are greatly influenced by Environment differences. Data is only for memo and reference.
Speed Measurement of one-way hashing algorithm