C语言实现获取文件MD5值

  #include

  #include

  #include

  ​

  typedef unsigned char uint8;

  typedef unsigned int uint32;

  ​

  // MD5常量定义

  const uint32 MD5_CONSTANTS[] = {

  0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,

  0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,

  0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,

  0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,

  0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,

  0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,

  0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,

  0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,

  0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,

  0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,

  0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,

  0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,

  0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,

  0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,

  0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,

  0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391

  };

  ​

  // 循环左移

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

  ​

  // 转换为大端字节序

  void to_big_endian(uint32 value, uint8* buffer) {

  buffer[0] = (uint8)(value & 0xff);

  buffer[1] = (uint8)((value >> 8) & 0xff);

  buffer[2] = (uint8)((value >> 16) & 0xff);

  buffer[3] = (uint8)((value >> 24) & 0xff);

  }

  ​

  // 处理消息块

  void process_block(const uint8* block, uint32* state) {

  uint32 a = state[0];

  uint32 b = state[1];

  uint32 c = state[2];

  uint32 d = state[3];

  uint32 m[16];

  ​

  // 将消息块划分为16个32位字,并进行字节序转换

  for (int i = 0; i < 16; i++) {

  m[i] = (((uint32)block[i * 4 + 0]) << 0) |

  (((uint32)block[i * 4 + 1]) << 8) |

  (((uint32)block[i * 4 + 2]) << 16) |

  (((uint32)block[i * 4 + 3]) << 24);

  }

  ​

  // MD5循环运算

  for (int i = 0; i < 64; i++) {

  uint32 f, g;

  ​

  if (i < 16) {

  f = (b & c) | ((~b) & d);

  g = i;

  } else if (i < 32) {

  f = (d & b) | ((~d) & c);

  g = (5 * i + 1) % 16;

  } else if (i < 48) {

  f = b ^ c ^ d;

  g = (3 * i + 5) % 16;

  } else {

  f = c ^ (b | (~d));

  g = (7 * i) % 16;

  }

  ​

  uint32 temp = d;

  d = c;

  c = b;

  b = b + LEFT_ROTATE((a + f + MD5_CONSTANTS[i] + m[g]), 7);

  a = temp;

  }

  ​

  // 更新状态

  state[0] += a;

  state[1] += b;

  state[2] += c;

  state[3] += d;

  }

  ​

  // 计算MD5值

  void calculate_md5(const uint8* message, size_t length, uint8* digest) {

  // 初始化状态

  uint32 state[4] = { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476 };

  ​

  // 填充消息

  size_t padded_length = ((length + 8) / 64 + 1) * 64;

  uint8* padded_message = (uint8*)calloc(padded_length, 1);

  memcpy(padded_message, message, length);

  padded_message[length] = 0x80;  // 添加一个1

  to_big_endian((uint32)(length * 8), padded_message + padded_length - 8);  // 添加长度(以位为单位)

  ​

  // 处理消息块

  for (size_t i = 0; i < padded_length; i += 64) {

  process_block(padded_message + i, state);

  }

  ​

  // 生成摘要

  for (int i = 0; i < 4; i++) {

  to_big_endian(state[i], digest + i * 4);

  }

  free(padded_message);

  }

  ​

  // 打印MD5值

  void print_md5(const uint8* digest) {

  for (int i = 0; i < 16; i++) {

  printf("%02x", digest[i]);

  }

  printf("

  ");

  }

  ​

  int main() {

  const char* message = "Hello, World!";

  size_t length = strlen(message);

  uint8 digest[16];

  ​

  calculate_md5((const uint8*)message, length, digest);

  printf("MD5: ");

  print_md5(digest);

  ​

  return 0;

  }