c++如何实现MD5算法

来源:爱站网时间:2020-06-04编辑:网友分享
MD5算法在C语言中是经常使用且常见的算法,但是对于c++如何实现MD5算法这个问题相信困扰了不少小伙伴们,那么接下来我们就一起去看看c++中MD5算法的实现代码。

MD5算法在C语言中是经常使用且常见的算法,但是对于c++如何实现MD5算法这个问题相信困扰了不少小伙伴们,那么接下来我们就一起去看看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_, 

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] = '\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 > (32-n));    
  // if x is signed, use: (x > (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 > 8*i) & 0xff;
  }
  for(int i = 4; i> 8*(i - 4)) & 0xff;
  }
  for(int i = 8; i> 8*(i - 8)) & 0xff;
  }
  for(int i = 12; i> 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  0)
  {
    UpdateMd5(padding, temp);
    totalInputBits_ -= (temp > 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);
}

以上就是小编介绍c++如何实现MD5算法的内容,有需要的用户,可以参照上述方法去使用,希望今天的分享能给大家带来帮助。

上一篇:C语言数据结构递归之斐波那契数列

下一篇:调用C/C++函数的方式

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载