MD5 算法

来源:互联网 发布:沈飞与空军恩怨 知乎 编辑:程序博客网 时间:2024/06/05 01:12

/*
  * md5 -- compute and check MD5 message digest.
  * this version only can calculate the char string.
  *
  * MD5 (Message-Digest algorithm 5) is a widely used, partially
  * insecure cryptographic hash function with a 128-bit hash value.
  *
  * Author: redraiment
  * Date: Aug 27, 2008
  * Version: 0.1.6
  */
  #include <stdlib.h>
  #include <string.h>
  #include <stdio.h>
  #include <math.h>
  #define SINGLE_ONE_BIT 0x80
  #define BLOCK_SIZE 512
  #define MOD_SIZE 448
  #define APP_SIZE 64
  #define BITS 8
  // MD5 Chaining Variable
  #define A 0x67452301UL
  #define B 0xEFCDAB89UL
  #define C 0x98BADCFEUL
  #define D 0x10325476UL
  // Creating own types
  #ifdef UINT64
  # undef UINT64
  #endif
  #ifdef UINT32
  # undef UINT32
  #endif
  typedef unsigned long long UINT64;
  typedef unsigned long UINT32;
  typedef unsigned char UINT8;
  typedef struct
  {
  char * message;
  UINT64 length;
  }STRING;
  const UINT32 X[4][2] = {{0, 1}, {1, 5}, {5, 3}, {0, 7}};
  // Constants for MD5 transform routine.
  const UINT32 S[4][4] = {
  { 7, 12, 17, 22 },
  { 5, 9, 14, 20 },
  { 4, 11, 16, 23 },
  { 6, 10, 15, 21 }
  };
  // F, G, H and I are basic MD5 functions.
  UINT32 F( UINT32 X, UINT32 Y, UINT32 Z )
  {
  return ( X & Y ) | ( ~X & Z );
  }
  UINT32 G( UINT32 X, UINT32 Y, UINT32 Z )
  {
  return ( X & Z ) | ( Y & ~Z );
  }
  UINT32 H( UINT32 X, UINT32 Y, UINT32 Z )
  {
  return X ^ Y ^ Z;
  }
  UINT32 I( UINT32 X, UINT32 Y, UINT32 Z )
  {
  return Y ^ ( X | ~Z );
  }
  // rotates x left s bits.
  UINT32 rotate_left( UINT32 x, UINT32 s )
  {
  return ( x << s ) | ( x >> ( 32 - s ) );
  }
  // Pre-processin
  UINT32 count_padding_bits ( UINT32 length )
  {
  UINT32 div = length * BITS / BLOCK_SIZE;
  UINT32 mod = length * BITS % BLOCK_SIZE;
  UINT32 c_bits;
  if ( mod == 0 )
  c_bits = BLOCK_SIZE;
  else
  c_bits = ( MOD_SIZE + BLOCK_SIZE - mod ) % BLOCK_SIZE;
  return c_bits / BITS;
  }
  STRING append_padding_bits ( char * argv )
  {
  UINT32 msg_length = strlen ( argv );
  UINT32 bit_length = count_padding_bits ( msg_length );
  UINT64 app_length = msg_length * BITS;
  STRING string;
  string.message = (char *)malloc(msg_length + bit_length + APP_SIZE / BITS);
  // Save message
  strncpy ( string.message, argv, msg_length );
  // Pad out to mod 64.
  memset ( string.message + msg_length, 0, bit_length );
  string.message [ msg_length ] = SINGLE_ONE_BIT;
  // Append length (before padding).
  memmove ( string.message + msg_length + bit_length, (char *)&app_length, sizeof( UINT64 ) );
  string.length = msg_length + bit_length + sizeof( UINT64 );
  return string;
  }
  int main ( int argc, char *argv[] )
  {
  STRING string;
  UINT32 w[16];
  UINT32 chain[4];
  UINT32 state[4];
  UINT8 r[16];
  UINT32 ( *auxi[ 4 ])( UINT32, UINT32, UINT32 ) = { F, G, H, I };
  int roundIdx;
  int argIdx;
  int sIdx;
  int wIdx;
  int i;
  int j;
  if ( argc < 2 )
  {
  fprintf ( stderr, "usage: %s string .../n", argv[ 0 ] );
  return EXIT_FAILURE;
  }
  for ( argIdx = 1; argIdx < argc; argIdx++ )
  {
  string = append_padding_bits ( argv[ argIdx ] );
  // MD5 initialization.
  chain[0] = A;
  chain[1] = B;
  chain[2] = C;
  chain[3] = D;
  for ( j = 0; j < string.length; j += BLOCK_SIZE / BITS)
  {
  memmove ( (char *)w, string.message + j, BLOCK_SIZE / BITS );
  memmove ( state, chain, sizeof(chain) );
  for ( roundIdx = 0; roundIdx < 4; roundIdx++ )
  {
  wIdx = X[ roundIdx ][ 0 ];
  sIdx = 0;
  for ( i = 0; i < 16; i++ )
  {
  // FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  // Rotation is separate from addition to prevent recomputation.
  state[sIdx] = state [ (sIdx + 1) % 4 ] +
  rotate_left ( state[sIdx] +
  ( *auxi[ roundIdx ] )
  ( state[(sIdx+1) % 4], state[(sIdx+2) % 4], state[(sIdx+3) % 4]) +
  w[ wIdx ] +
  (UINT32)floor( (1ULL << 32) * fabs(sin( roundIdx * 16 + i + 1 )) ),
  S[ roundIdx ][ i % 4 ]);
  sIdx = ( sIdx + 3 ) % 4;
  wIdx = ( wIdx + X[ roundIdx ][ 1 ] ) & 0xF;
  }
  }
  chain[ 0 ] += state[ 0 ];
  chain[ 1 ] += state[ 1 ];
  chain[ 2 ] += state[ 2 ];
  chain[ 3 ] += state[ 3 ];
  }
  memmove ( r + 0, (char *)&chain[0], sizeof(UINT32) );
  memmove ( r + 4, (char *)&chain[1], sizeof(UINT32) );
  memmove ( r + 8, (char *)&chain[2], sizeof(UINT32) );
  memmove ( r + 12, (char *)&chain[3], sizeof(UINT32) );
  for ( i = 0; i < 16; i++ )
  printf ( "%02x", r[i] );
  putchar ( '/n' );
  }
  return EXIT_SUCCESS;
  }

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 5岁宝宝咳嗽有痰怎么办 生川乌外贴中毒怎么办 7个月宝宝流鼻涕怎么办 脸上皮肤毛孔粗大有黑头怎么办 嗓子长了个囊肿怎么办 食管胃粘膜异位怎么办 狗狗肿瘤破了怎么办 婴儿胃食道反流怎么办 小儿胃食道反流怎么办 放疗后咳嗽痰多怎么办 胃息肉是恶性的怎么办 老是胃疼,胃胀怎么办 吃完饭之后胃胀怎么办 胃痛胃胀怎么办能缓解 胃胀不消化怎么办快速解决 便秘肛裂了好痛怎么办 胃消化慢还便秘怎么办 11个月婴儿便秘怎么办 80多岁老人便秘怎么办 狗狗便秘拉不出来怎么办 2个月幼犬便秘怎么办 狗狗便秘怎么办吃什么 痔疮又痛又痒怎么办 痔疮肉球特别痒怎么办 长了个小痔疮怎么办 产后4天没大便怎么办 7个月孕妇痔疮怎么办 运动完恶心想吐怎么办 跑步后恶心想吐怎么办 肠子不蠕动严重便秘怎么办 怀孕八个月严重便秘怎么办 怀孕七个月便秘严重怎么办 怀孕两个月便秘严重怎么办 3岁宝宝上火便秘怎么办 7个月的宝宝贫血怎么办 9个月婴儿贫血怎么办 肛裂大便有血怎么办 生完宝宝肛门痛怎么办 肛周脓肿出血了怎么办 胃胀怎么办简单的办法 吃多了胃胀难受怎么办