亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > C++ > 正文

c++實現MD5算法實現代碼

2020-01-26 15:40:56
字體:
來源:轉載
供稿:網友

測試結果和百度百科測試例子一致。

實現過程中需要注意事項:最后把四個變量A B C D 鏈接成結果時 ,注意變量高低位的先后順序,具體參考 LinkResult()方法。

md5.h

#ifndef _MD5_H_#define _MD5_H_#include <iostream>#include <string>using namespace std;class MD5{  public:    typedef unsigned char uchar8; //make sure it is 8bit    typedef char char8; //make sure it is 8bit    MD5();        void init();    void UpdateMd5(const uchar8 input[], const int length);       void UpdateMd5(const char8 input[], const int length);           void Finalize();        void ComputMd5(const uchar8 input[], const int length);     void ComputMd5(const char8 input[], const int length);         string GetMd5();        void printMd5();          private:    typedef unsigned int uint32;    //make sure it is 32 bit;    typedef unsigned long long uint64; //make sure it is 64 bit;    uint32 A, B, C, D;    const static int blockLen_ = 64;  // 512/8                     //the remain after last updata (because md5 may be computed segment by segment)    uchar8 remain_[blockLen_];              int remainNum_ ;     // the number of remain_, < 64     uint64 totalInputBits_;    uchar8 md5Result_[16];  //bit style md5 result,totally 128 bit    char md5Result_hex_[33]; //hexadecimal style result; md5Result_hex_[32]='/0'    bool isDone_;      // indicate the comput is finished;              inline uint32 RotateLeft(const uint32 x, int n);    inline uint32 F(const uint32 x, const uint32 y, const uint32 z);    inline uint32 G(const uint32 x, const uint32 y, const uint32 z);    inline uint32 H(const uint32 x, const uint32 y, const uint32 z);    inline uint32 I(const uint32 x, const uint32 y, const uint32 z);    inline void FF(uint32 &a, const uint32 b, const uint32 c, const uint32 d,            const uint32 Mj, const int s, const uint32 ti);    inline void GG(uint32 &a, const uint32 b, const uint32 c, const uint32 d,            const uint32 Mj, const int s, const uint32 ti);    inline void HH(uint32 &a, const uint32 b, const uint32 c, const uint32 d,            const uint32 Mj, const int s, const uint32 ti);    inline void II(uint32 &a, const uint32 b, const uint32 c, const uint32 d,            const uint32 Mj, const int s, const uint32 ti);                void UcharToUint(uint32 output[], const uchar8 input[], const unsigned int transLength);        void FourRound(const uchar8 block[]);      void LinkResult();          };/* user guide  you can comput the md5 by using the funtion ComputMd5  eg:    MD5 m;    MD5::char8 str[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";    m.ComputMd5(str,sizeof(str) - 1);      m.printMd5();  if you want to comput segment by segment,you can do as follow, and init() is suggested   the begging,and Finalize() must call in the end:        MD5 M;    m.init();    MD5::uchar8 str1[] = "ABCDEFGHIJKLMN";    MD5::uchar8 str2[] = "OPQRSTUVWXYZabcdefghijk";    MD5::uchar8 str3[] = "lmnopqrstuvwxyz";    m.UpdateMd5(str1,sizeof(str1) - 1);    m.UpdateMd5(str2,sizeof(str2) - 1);    m.UpdateMd5(str3,sizeof(str3) - 1);    m.Finalize();    m.printMd5();  if you want to comput the md5 of a file, you can use the interface of this program.*/#endif

md5.cpp

#include"md5.h"#include<iostream>using namespace std;const int S[4][4] = {7, 12, 17, 22,           5, 9, 14, 20,           4, 11, 16, 23,           6, 10, 15, 21};void MD5::init(){  A = 0x67452301;  B = 0xefcdab89;  C = 0x98badcfe;  D = 0x10325476;  remainNum_ = 0;  remain_[0] = '/0';  md5Result_hex_[0] = '/0';  md5Result_[0] = '/0';  totalInputBits_ = 0;  isDone_ = false;}MD5::MD5(){  init();}inline MD5::uint32 MD5::RotateLeft(const uint32 x, int n){  return (x << n) | (x >> (32-n));      // if x is signed, use: (x << n) | ((x & 0xFFFFFFFF) >> (32-n))}inline MD5::uint32 MD5::F(const uint32 x, const uint32 y, const uint32 z){  return (x & y) | ((~x) & z);}inline MD5::uint32 MD5::G(const uint32 x, const uint32 y, const uint32 z){  return (x & z) | (y & (~z));}inline MD5::uint32 MD5::H(const uint32 x, const uint32 y, const uint32 z){  return x ^ y ^ z;}inline MD5::uint32 MD5::I(const uint32 x, const uint32 y, const uint32 z){  return y ^ (x | (~z));}inline void MD5::FF(uint32 &a, const uint32 b, const uint32 c, const uint32 d,          const uint32 Mj, const int s, const uint32 ti){  a = b + RotateLeft(a + F(b, c, d) + Mj + ti, s);}   inline void MD5::GG(uint32 &a, const uint32 b, const uint32 c, const uint32 d,          const uint32 Mj, const int s, const uint32 ti){  a = b + RotateLeft(a + G(b, c, d) + Mj + ti, s);}          inline void MD5::HH(uint32 &a, const uint32 b, const uint32 c, const uint32 d,          const uint32 Mj, const int s, const uint32 ti){  a = b + RotateLeft(a + H(b, c, d) + Mj + ti, s);}          inline void MD5::II(uint32 &a, const uint32 b, const uint32 c, const uint32 d,          const uint32 Mj, const int s, const uint32 ti){  a = b + RotateLeft(a + I(b, c, d) + Mj + ti, s);}     // link A B C D to result(bit style result and hexadecimal style result)void MD5::LinkResult(){  //bit style result  for(int i = 0; i < 4; i++) //link A: low to high  {    md5Result_[i] = (A >> 8*i) & 0xff;  }  for(int i = 4; i<8; i++)  //link B: low to high  {    md5Result_[i] = (B >> 8*(i - 4)) & 0xff;  }  for(int i = 8; i<12; i++) //link C: low to high  {    md5Result_[i] = (C >> 8*(i - 8)) & 0xff;  }  for(int i = 12; i<16; i++) //link D: low to high  {    md5Result_[i] = (D >> 8*(i - 12)) & 0xff;  }  //change to hexadecimal style result  // note: it is not the same as simply link hex(A) hex(B) hex(C) hex(D)  for(int i = 0; i < 16; i++)    sprintf(& md5Result_hex_[i*2], "%02x", md5Result_[i]);  md5Result_hex_[32] = '/0';}//print the md5 by hexvoid MD5::printMd5(){  if(!isDone_)  {    cout<< "Error: computation is not finished" <<endl;      }  else    cout<< "MD5 Value: " << md5Result_hex_ <<endl;}//get the md5 value of hex style string MD5::GetMd5(){  if(!isDone_)  {    cout<< "Error: computation is not finished" <<endl;    exit(0);  }  string a((const char *)md5Result_hex_);  return a;}void MD5::UcharToUint(uint32 output[], const uchar8 input[], const unsigned int transLength)     {  for(int i = 0, j = 0; j < transLength; i++, j += 4)  {    output[i] = ((uint32)input[j]) | (((uint32)input[j+1]) << 8) |        (((uint32)input[j+2]) << 16) | (((uint32)input[j+3]) << 24);  }}// four round on a block of 512 bits;void MD5::FourRound(const uchar8 block[]){  uint32 a = A, b = B, c = C, d = D;  uint32 M[16];  UcharToUint(M, block, blockLen_); //blockLen_ is a const int =64;    //round 1  FF (a, b, c, d, M[ 0], S[0][0], 0xd76aa478);   FF (d, a, b, c, M[ 1], S[0][1], 0xe8c7b756);   FF (c, d, a, b, M[ 2], S[0][2], 0x242070db);   FF (b, c, d, a, M[ 3], S[0][3], 0xc1bdceee);   FF (a, b, c, d, M[ 4], S[0][0], 0xf57c0faf);   FF (d, a, b, c, M[ 5], S[0][1], 0x4787c62a);   FF (c, d, a, b, M[ 6], S[0][2], 0xa8304613);  FF (b, c, d, a, M[ 7], S[0][3], 0xfd469501);  FF (a, b, c, d, M[ 8], S[0][0], 0x698098d8);   FF (d, a, b, c, M[ 9], S[0][1], 0x8b44f7af);  FF (c, d, a, b, M[10], S[0][2], 0xffff5bb1);   FF (b, c, d, a, M[11], S[0][3], 0x895cd7be);   FF (a, b, c, d, M[12], S[0][0], 0x6b901122);  FF (d, a, b, c, M[13], S[0][1], 0xfd987193);  FF (c, d, a, b, M[14], S[0][2], 0xa679438e);  FF (b, c, d, a, M[15], S[0][3], 0x49b40821);   // round 2   GG (a, b, c, d, M[ 1], S[1][0], 0xf61e2562);   GG (d, a, b, c, M[ 6], S[1][1], 0xc040b340);   GG (c, d, a, b, M[11], S[1][2], 0x265e5a51);   GG (b, c, d, a, M[ 0], S[1][3], 0xe9b6c7aa);  GG (a, b, c, d, M[ 5], S[1][0], 0xd62f105d);   GG (d, a, b, c, M[10], S[1][1], 0x2441453);   GG (c, d, a, b, M[15], S[1][2], 0xd8a1e681);  GG (b, c, d, a, M[ 4], S[1][3], 0xe7d3fbc8);   GG (a, b, c, d, M[ 9], S[1][0], 0x21e1cde6);  GG (d, a, b, c, M[14], S[1][1], 0xc33707d6);  GG (c, d, a, b, M[ 3], S[1][2], 0xf4d50d87);  GG (b, c, d, a, M[ 8], S[1][3], 0x455a14ed);   GG (a, b, c, d, M[13], S[1][0], 0xa9e3e905);   GG (d, a, b, c, M[ 2], S[1][1], 0xfcefa3f8);   GG (c, d, a, b, M[ 7], S[1][2], 0x676f02d9);  GG (b, c, d, a, M[12], S[1][3], 0x8d2a4c8a);  //round 3   HH (a, b, c, d, M[ 5], S[2][0], 0xfffa3942);   HH (d, a, b, c, M[ 8], S[2][1], 0x8771f681);   HH (c, d, a, b, M[11], S[2][2], 0x6d9d6122);   HH (b, c, d, a, M[14], S[2][3], 0xfde5380c);  HH (a, b, c, d, M[ 1], S[2][0], 0xa4beea44);   HH (d, a, b, c, M[ 4], S[2][1], 0x4bdecfa9);  HH (c, d, a, b, M[ 7], S[2][2], 0xf6bb4b60);   HH (b, c, d, a, M[10], S[2][3], 0xbebfbc70);   HH (a, b, c, d, M[13], S[2][0], 0x289b7ec6);   HH (d, a, b, c, M[ 0], S[2][1], 0xeaa127fa);   HH (c, d, a, b, M[ 3], S[2][2], 0xd4ef3085);  HH (b, c, d, a, M[ 6], S[2][3], 0x4881d05);   HH (a, b, c, d, M[ 9], S[2][0], 0xd9d4d039);   HH (d, a, b, c, M[12], S[2][1], 0xe6db99e5);   HH (c, d, a, b, M[15], S[2][2], 0x1fa27cf8);   HH (b, c, d, a, M[ 2], S[2][3], 0xc4ac5665);    //round 4   II (a, b, c, d, M[ 0], S[3][0], 0xf4292244);   II (d, a, b, c, M[ 7], S[3][1], 0x432aff97);   II (c, d, a, b, M[14], S[3][2], 0xab9423a7);   II (b, c, d, a, M[ 5], S[3][3], 0xfc93a039);  II (a, b, c, d, M[12], S[3][0], 0x655b59c3);   II (d, a, b, c, M[ 3], S[3][1], 0x8f0ccc92);   II (c, d, a, b, M[10], S[3][2], 0xffeff47d);   II (b, c, d, a, M[ 1], S[3][3], 0x85845dd1);   II (a, b, c, d, M[ 8], S[3][0], 0x6fa87e4f);   II (d, a, b, c, M[15], S[3][1], 0xfe2ce6e0);   II (c, d, a, b, M[ 6], S[3][2], 0xa3014314);   II (b, c, d, a, M[13], S[3][3], 0x4e0811a1);   II (a, b, c, d, M[ 4], S[3][0], 0xf7537e82);   II (d, a, b, c, M[11], S[3][1], 0xbd3af235);   II (c, d, a, b, M[ 2], S[3][2], 0x2ad7d2bb);  II (b, c, d, a, M[ 9], S[3][3], 0xeb86d391);    A += a;  B += b;  C += c;  D += d;}// update md5,must consider the remain_.void MD5::UpdateMd5(const uchar8 input[], const int length){    isDone_ = false;  totalInputBits_ += (length << 3);    int start = blockLen_ - remainNum_; //blockLen_ = 64  //copy a part of input to remain_ so it can form a block(size=64)   if(start <= length)  {    // can form a block,then do FourRound to this block      memcpy(&remain_[remainNum_], input, start) ;      FourRound(remain_);      int i;      for(i = start; i <= length - blockLen_; i += blockLen_)      {        FourRound(&input[i]);      }      remainNum_ = length - i;       memcpy(remain_, &input[i], remainNum_);   }     else  {    // can not form a block, function return;    memcpy(&remain_[remainNum_], input, length);    remainNum_ += length;  }    }void MD5::UpdateMd5(const char8 input[], const int length){  UpdateMd5((const uchar8 *)input, length);}// padding with 100000... to remain_ and add the 64bit original size of input void MD5::Finalize(){  if(isDone_ == true)    return;      uchar8 padding[64] = {  0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  };   int temp = 56 - remainNum_; //56 = 448/8  if(temp > 0)  {    UpdateMd5(padding, temp);    totalInputBits_ -= (temp << 3);  }  else if(temp < 0)  {    UpdateMd5(padding, 64 + temp);    totalInputBits_ -= ((64 + temp) << 3);  }   // trans totalInputBits_ to uchar8 (64bits)  uchar8 Bits[8];  for(int i = 0; i < 8; i++)  {    Bits[i] = (totalInputBits_ >> 8*i) & 0xff;  }    UpdateMd5(Bits, 8); // add the number of original input (the last 64bits)    LinkResult();  isDone_ = true;}// comput the md5 based on input, (just this one input)void MD5::ComputMd5(const uchar8 input[], const int length){  init();  UpdateMd5(input, length);  Finalize();}void MD5::ComputMd5(const char8 input[], const int length){  ComputMd5((const uchar8 *)input, length);}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美—级高清免费播放| 超薄丝袜一区二区| 国产成人极品视频| 国产精品久久综合av爱欲tv| 日本亚洲欧美三级| 一区二区三区视频在线| 色偷偷88888欧美精品久久久| 亚洲人线精品午夜| 欧美—级高清免费播放| 亚洲第一精品自拍| 亚洲精品永久免费精品| 欧美日韩亚洲一区二区| 色噜噜狠狠狠综合曰曰曰88av| 一区二区三区视频免费| 91av免费观看91av精品在线| 日韩av在线电影网| 国产精品大片wwwwww| 亚洲电影免费观看高清| 亚洲欧美国产精品va在线观看| 92福利视频午夜1000合集在线观看| 在线日韩av观看| 欧美巨大黑人极品精男| 国产欧美一区二区白浆黑人| 久久久久久久久久婷婷| 91成人天堂久久成人| 日韩av在线网址| 精品欧美国产一区二区三区| 美女性感视频久久久| 日韩有码片在线观看| 精品久久久久久亚洲精品| 国产精品成人在线| 欧美裸体xxxx极品少妇软件| 久久人人爽人人爽人人片亚洲| 国产精品免费在线免费| 欧美亚洲第一区| 欧美日韩xxx| 亚洲欧美另类自拍| 精品福利在线视频| 国产精品视频免费在线观看| 欧美日韩亚洲网| 亚洲第一福利在线观看| 亚洲国产精品一区二区三区| 九色成人免费视频| 欧美激情精品久久久久久变态| 亚洲国产精品美女| 日韩欧美在线播放| 欧美成人一区二区三区电影| 亚洲国产精品va在线观看黑人| 欧美成人免费在线观看| 国产香蕉一区二区三区在线视频| 日本免费一区二区三区视频观看| 国产精品高潮粉嫩av| 亚洲新声在线观看| 91日本在线视频| 欧美日韩一区二区免费视频| 另类美女黄大片| 欧美性jizz18性欧美| 亚洲精品欧美日韩| 国产日韩中文字幕在线| 成人免费自拍视频| 91在线国产电影| 久久夜色精品国产亚洲aⅴ| 国产欧美一区二区三区在线看| 欧美高清在线视频观看不卡| 欧美一级淫片videoshd| www.欧美精品一二三区| 亚洲精品久久久久久久久久久久久| 欧美激情视频在线观看| 亚洲新中文字幕| 伊人久久免费视频| 国产成人精品视频在线观看| 欧美中文字幕在线视频| 日韩视频免费中文字幕| 亚洲区免费影片| 欧美成aaa人片在线观看蜜臀| 亚洲高清av在线| 欧美一级高清免费播放| 精品一区精品二区| 欧美在线视频网| 欧美第一黄网免费网站| 亚洲白拍色综合图区| 国产一区二区视频在线观看| 亚洲少妇激情视频| 91久久在线观看| 日韩中文字幕亚洲| 国产日韩在线免费| 性色av香蕉一区二区| 欧美电影免费观看高清| 久久亚洲精品网站| 午夜精品一区二区三区在线视频| 97激碰免费视频| 精品电影在线观看| 久久久免费观看视频| 欧美精品在线播放| 日本国产精品视频| 国产精品久久久久久久久久小说| 国产日韩欧美中文在线播放| 国产91精品久久久久| 97超碰国产精品女人人人爽| 欧美性猛交xxxx乱大交极品| 日韩欧美在线视频观看| 国产丝袜一区二区| 久久成人人人人精品欧| 波霸ol色综合久久| 国产aⅴ夜夜欢一区二区三区| 亚洲第一精品夜夜躁人人躁| 成人精品视频在线| 日韩av在线免费观看一区| 欧美激情亚洲视频| 日韩av三级在线观看| 亚洲欧美www| 精品福利一区二区| 欧美午夜精品久久久久久人妖| 国产欧美中文字幕| 成人写真福利网| 欧美在线视频观看| 国产精品流白浆视频| 国产精品久久电影观看| 久久91精品国产91久久久| 欧美日韩国内自拍| 亚洲成人av片在线观看| 国产精品成人av在线| 成人免费视频网址| 一色桃子一区二区| 国产精品视频久久| 亚洲精品免费在线视频| 国产精品1区2区在线观看| 欧美激情奇米色| 久久69精品久久久久久国产越南| 精品精品国产国产自在线| 国产一区二中文字幕在线看| 欧美日韩激情视频| 欧美日韩国产精品专区| 九九热视频这里只有精品| 91久久精品日日躁夜夜躁国产| 亚洲欧美一区二区三区久久| 亚洲深夜福利视频| 日韩欧美一区视频| 欧美综合在线第二页| 日韩成人小视频| 欧美日韩美女在线观看| 欧美精品成人91久久久久久久| 国产日本欧美视频| 97成人在线视频| 另类美女黄大片| 国产精品海角社区在线观看| 中文字幕亚洲欧美日韩高清| 深夜精品寂寞黄网站在线观看| 91精品国产综合久久香蕉922| 97免费视频在线播放| 国产午夜精品免费一区二区三区| 中文字幕日韩视频| 最近中文字幕日韩精品| 国产精品扒开腿爽爽爽视频| 精品久久香蕉国产线看观看亚洲| 久久视频在线免费观看| 动漫精品一区二区| 国产精品视频男人的天堂| 在线观看视频99| 欧美日本精品在线| 日韩成人中文字幕在线观看| 国产a级全部精品| 国产www精品| 亚洲人av在线影院|