栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++实现MD5算法实现代码

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++实现MD5算法实现代码

测试结果和百度百科测试例子一致。

实现过程中需要注意事项:最后把四个变量A B C D 链接成结果时 ,注意变量高低位的先后顺序,具体参考 linkResult()方法。

md5.h

#ifndef _MD5_H_
#define _MD5_H_

#include 
#include 
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]=''
    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();
   
};



#endif

md5.cpp

#include"md5.h"

#include
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] = '';
  md5Result_hex_[0] = '';
  md5Result_[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] = '';

}

//print the md5 by hex
void MD5::printMd5()
{
  if(!isDone_)
  {
    cout<< "Error: computation is not finished" < 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);
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/65975.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号