Speed Measurement of one-way hashing algorithm

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.