不使用库计算空字符串的MD5摘要

我正在尝试学习MD5算法并“从头开始”编写c程序。我要计算空字符串的MD5摘要(然后从那里开始使用)。

文档(对应的RFC)似乎很令人困惑,但我认为我已经遵循了。我复制了函数和常量的定义(直到main()为止的所有内容)和Rounds本身。我的输入只是所需的填充(因为输入是空字符串),它是一个1位,后跟511 0位(消息的表示应全部为0

state[4]是缓冲区数组。

我将输入复制到unsigned long int数组中,以便可以将其视为函数的整数,然后将其复制回字符数组。

#include <stdio.h>
#include <math.h>
#include <string.h>

typedef unsigned char *POINTER;
typedef unsigned short int UINT2;
typedef unsigned long int UINT4;

#define S11 7
#define S12 12
#define S13 17
#define S14 22
#define S21 5
#define S22 9
#define S23 14
#define S24 20
#define S31 4
#define S32 11
#define S33 16
#define S34 23
#define S41 6
#define S42 10
#define S43 15
#define S44 21

#define F(x,y,z) ((x & y) | (~x & z))
#define G(x,z) ((x & z) | (~z & y))
#define H(x,z) (x ^ y ^ z)
#define I(x,z) (y ^ (x | ~z))

#define ROTATE_LEFT(x,n) (((x) << (n)) | ((x) >> (32-(n))))

#define FF(a,b,c,d,x,s,ac) { \
 (a) += F ((b),(c),(d)) + (x) + (UINT4)(ac); \
 (a) = ROTATE_LEFT ((a),(s)); \
 (a) += (b); \
  }
#define GG(a,ac) { \
 (a) += G ((b),(s)); \
 (a) += (b); \
  }
#define HH(a,ac) { \
 (a) += H ((b),(s)); \
 (a) += (b); \
  }
#define II(a,ac) { \
 (a) += I ((b),(s)); \
 (a) += (b); \
  }

int main () {

int i;
char input[64]={0};
char output[64]={0};
unsigned long int x[16]={0};
unsigned long int state[4]={0x67452301,0xefcdab89,0x98badcfe,0x10325476};

//Initialize variables:
int a = state[0];   //A
int b = state[1];   //B
int c = state[2];   //C
int d = state[3];   //D

input[0]=0x80;

memcpy(x,input,64);

/* Round 1 */
FF (a,x[ 0],S11,0xd76aa478); /* 1 */
FF (d,a,x[ 1],S12,0xe8c7b756); /* 2 */
FF (c,x[ 2],S13,0x242070db); /* 3 */
FF (b,x[ 3],S14,0xc1bdceee); /* 4 */
FF (a,x[ 4],0xf57c0faf); /* 5 */
FF (d,x[ 5],0x4787c62a); /* 6 */
FF (c,x[ 6],0xa8304613); /* 7 */
FF (b,x[ 7],0xfd469501); /* 8 */
FF (a,x[ 8],0x698098d8); /* 9 */
FF (d,x[ 9],0x8b44f7af); /* 10 */
FF (c,x[10],0xffff5bb1); /* 11 */
FF (b,x[11],0x895cd7be); /* 12 */
FF (a,x[12],0x6b901122); /* 13 */
FF (d,x[13],0xfd987193); /* 14 */
FF (c,x[14],0xa679438e); /* 15 */
FF (b,x[15],0x49b40821); /* 16 */

/* Round 2 */
GG (a,S21,0xf61e2562); /* 17 */
GG (d,S22,0xc040b340); /* 18 */
GG (c,S23,0x265e5a51); /* 19 */
GG (b,S24,0xe9b6c7aa); /* 20 */
GG (a,0xd62f105d); /* 21 */
GG (d,0x2441453); /* 22 */
GG (c,0xd8a1e681); /* 23 */
GG (b,0xe7d3fbc8); /* 24 */
GG (a,0x21e1cde6); /* 25 */
GG (d,0xc33707d6); /* 26 */
GG (c,0xf4d50d87); /* 27 */
GG (b,0x455a14ed); /* 28 */
GG (a,0xa9e3e905); /* 29 */
GG (d,0xfcefa3f8); /* 30 */
GG (c,0x676f02d9); /* 31 */
GG (b,0x8d2a4c8a); /* 32 */

/* Round 3 */
HH (a,S31,0xfffa3942); /* 33 */
HH (d,S32,0x8771f681); /* 34 */
HH (c,S33,0x6d9d6122); /* 35 */
HH (b,S34,0xfde5380c); /* 36 */
HH (a,0xa4beea44); /* 37 */
HH (d,0x4bdecfa9); /* 38 */
HH (c,0xf6bb4b60); /* 39 */
HH (b,0xbebfbc70); /* 40 */
HH (a,0x289b7ec6); /* 41 */
HH (d,0xeaa127fa); /* 42 */
HH (c,0xd4ef3085); /* 43 */
HH (b,0x4881d05); /* 44 */
HH (a,0xd9d4d039); /* 45 */
HH (d,0xe6db99e5); /* 46 */
HH (c,0x1fa27cf8); /* 47 */
HH (b,0xc4ac5665); /* 48 */

/* Round 4 */
II (a,S41,0xf4292244); /* 49 */
II (d,S42,0x432aff97); /* 50 */
II (c,S43,0xab9423a7); /* 51 */
II (b,S44,0xfc93a039); /* 52 */
II (a,0x655b59c3); /* 53 */
II (d,0x8f0ccc92); /* 54 */
II (c,0xffeff47d); /* 55 */
II (b,0x85845dd1); /* 56 */
II (a,0x6fa87e4f); /* 57 */
II (d,0xfe2ce6e0); /* 58 */
II (c,0xa3014314); /* 59 */
II (b,0x4e0811a1); /* 60 */
II (a,0xf7537e82); /* 61 */
II (d,0xbd3af235); /* 62 */
II (c,0x2ad7d2bb); /* 63 */
II (b,0xeb86d391); /* 64 */

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

memcpy(output,state,64);

for (i=0;i<=31;i++) {
    printf("%02x ",output[2*i]);
}

return 0;
}

输出为

ffffff85 ffffff94 58 fffffff9 3f ffffffed ffffff87 ffffffc1 ffffff80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 

(可以想象)何时应该是空字符串的MD5摘要

d4 1d 8c d9 8f 00 b2 04 e9 80 09 98 ec f8 42 7e
piaodongdeyun1225 回答:不使用库计算空字符串的MD5摘要

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3160835.html

大家都在问