如何用c / c ++填充我的md5消息

时间:2012-09-07 02:07:29

标签: c++ c md5

我正在使用c ++编写一个程序来执行md5校验和。我这样做主要是因为我认为我将学习很多关于c ++,校验和,OOP以及我遇到的其他任何事情。

我遇到了校验和问题,我认为问题在于填充消息填充的函数padbuff。

#include "HashMD5.h"

int leftrotate(int x, int y);
void padbuff(uchar * buffer);

//HashMD5 constructor
HashMD5::HashMD5()
{
  Type = "md5";
  Hash = "";
}

HashMD5::HashMD5(const char * hashfile)
{
  Type = "md5";
  std::ifstream filestr;
  filestr.open(hashfile, std::fstream::in | std::fstream::binary);
  if(filestr.fail())
  {
    std::cerr << "File " << hashfile << " was not opened.\n";
    std::cerr << "Open failed with error ";
  }
}

std::string HashMD5::GetType()
{
  return this->Type;
}

std::string HashMD5::GetHash()
{
  return this->Hash;
}

bool HashMD5::is_open()
{
  return !((this->filestr).fail());
}

void HashMD5::CalcHash(unsigned int * hash)
{
  unsigned int *r, *k;
  int r2[4] = {0, 4, 9, 15};
  int r3[4] = {0, 7, 12, 19};
  int r4[4] = {0, 4, 9, 15};
  uchar * buffer;
  int bufLength = (2<<20)*8;
  int f,g,a,b,c,d, temp;
  int *head;
  uint32_t maxint = 1<<31;

  //Initialized states
  unsigned int h[4]{ 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476};

  r = new unsigned int[64];
  k = new unsigned int[64];
  buffer = new uchar[bufLength];
  if(r==NULL || k==NULL || buffer==NULL)
  {
    std::cerr << "One of the dyn alloc failed\n";
  }

  // r specifies the per-round shift amounts
  for(int i = 0; i<16; i++)
    r[i] = 7 + (5 * ((i)%4) );

  for(int i = 16; i < 32; i++)
    r[i] = 5 + r2[i%4];

  for(int i = 32; i< 48; i++)
    r[i] = 4 + r3[i%4];

  for(int i = 48; i < 63; i++)
    r[i] = 6 + r4[i%4];

  for(int i = 0; i < 63; i++)
  {
    k[i] = floor( fabs( sin(i + 1)) * maxint);
  }

  while(!(this->filestr).eof())
  {
    //Read in 512 bits
    (this->filestr).read((char *)buffer, bufLength-512);

    padbuff(buffer);

    //The 512 bits are now 16 32-bit ints
    head = (int *)buffer;

    for(int i = 0; i < 64; i++)
    {
      if(i >=0 && i <=15)
      {
        f = (b & c) | (~b & d);
        g = i;
      }
      else if(i >= 16 && i <=31)
      {
        f = (d & b) | (~d & b);
        g = (5*i +1) % 16;
      }
      else if(i >=32 && i<=47)
      {
        f = b ^ c ^ d;
        g = (3*i + 5 ) % 16;
      }
      else
      {
        f = c ^ (b | ~d);
        g = (7*i) % 16;
      }

      temp = d;
      d = c;
      c = b;
      b = b + leftrotate((a + f + k[i] + head[g]), r[i]);
      a = temp;

    }

    h[0] +=a;
    h[1] +=b;
    h[2] +=c;
    h[3] +=d;         
  }


  delete[] r;
  delete[] k;

  hash = h;
}

int leftrotate(int x, int y)
{
   return(x<<y) | (x >> (32 -y));
}

void padbuff(uchar* buffer)
{
   int lack;
   int length = strlen((char *)buffer);
   uint64_t mes_size = length % UINT64_MAX;
   if((lack = (112 - (length % 128) ))>0)
   {
     *(buffer + length) = ('\0'+1 ) << 3;
     memset((buffer + length + 1),0x0,lack);  
     memcpy((void*)(buffer+112),(void *)&mes_size, 64);
   }

}

在我的测试程序中,我在一条空消息上运行它。因此,padbuff中的length为0.然后当我执行*(buffer + length) = ('\0'+1 ) << 3;时,我尝试用1填充消息。在Netbeans调试器中,我将buffer转换为uint64_t它说buffer=8。我试图将1 bit放在最重要的缓冲区中,所以我的演员应该是UINT64_MAX。它没有,所以我对我的填充代码如何工作感到困惑。有人能告诉我我在做什么以及我应该在padbuff做什么?谢谢,我为这个长期存在的问题道歉。

为了清楚填充应该做什么,这里是维基百科的填充摘录: 填充消息以使其长度可被512整除。填充的工作方式如下:首先将单个位1附加到消息的末尾。接下来是使消息长度比512的倍数少64位所需的零。其余的位用64位填充,表示原始消息的长度,模264。

我主要是寻求padbuff的帮助,但是因为我正在努力学习所有的评论都很受欢迎。

1 个答案:

答案 0 :(得分:1)

第一个问题是你做了什么:

  1. length % UINT64_MAX根本没有意义,因为长度以字节为单位,MAX是您可以在UINT64中存储的值。
  2. 您认为将1位置于最高位会产生最大值。实际上,你需要在所有位中加1才能得到它。
  3. 你将1移动3.它只是字节长度的一半。
  4. 缓冲区指向的字节在little endian中最不重要。 (我假设你有一个小端,因为调试器显示8)。
  5. 第二个问题它应该如何运作。 我不知道padbuff应该做什么,但是如果你想填充并获得UINT64_MAX,你需要这样的东西:

    int length = strlen((char *)buffer);
    int len_of_padding = sizeof(uint64_t) - length % sizeof(uint64_t);
    if(len_of_padding > 0)
    {
      memset((void*)(buffer + length), 0xFF, len_of_padding);
    }
    

    您使用了两个uint64值的长度。可能你想把下一个归零:

    uint64_t *after = (uint64_t*)(buffer + length + len_of_padding);
    *after = 0;