⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 um_md5.c

📁 hammerOS(了解的人就知道了)的一个板块Manage的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* MD5lib.h - md5 library
 */

/* Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
rights reserved.

RSA Data Security, Inc. makes no representations concerning either
the merchantability of this software or the suitability of this
software for any particular purpose. It is provided "as is"
without express or implied warranty of any kind.

These notices must be retained in any copies of any part of this
documentation and/or software.
 */

/* The following makes MD default to MD5 if it has not already been
  defined with C compiler flags.
 */

#ifdef _cplusplus
extern"C"{
#endif



#include <hos_type.h>

#define _UM_MD_ 5

/* GLOBAL.H - RSAREF types and constants
 */

/* PROTOTYPES should be set to one if and only if the compiler supports
  function argument prototyping.
  The following makes PROTOTYPES default to 0 if it has not already
  been defined with C compiler flags.
 */

/* um_md5_pointer defines a generic um_md5_pointer type */
typedef unsigned char *um_md5_pointer;

/* um_md5_uint2 defines a two byte word */
typedef unsigned short int um_md5_uint2;

/* um_md5_uint4 defines a four byte word */
typedef unsigned long int um_md5_uint4;

/* _UM_MD5_PROTO_LIST_ is defined depending on how PROTOTYPES is defined above.
If using PROTOTYPES, then _UM_MD5_PROTO_LIST_ returns the list, otherwise it
  returns an empty list.
 */
#define _UM_MD5_PROTO_LIST_(list) ()


 /* Length of test block, number of test blocks.
 */
#define _UM_MD5_TEST_BLOCK_LEN 1000
#define _UM_MD5_TEST_BLOCK_COUNT_ 1000

  

/* Constants for MD5Transform routine.
 */
#define _UM_MD5_S11_ 7
#define _UM_MD5_S12_ 12
#define _UM_MD5_S13_ 17
#define _UM_MD5_S14_ 22
#define _UM_MD5_S21_ 5
#define _UM_MD5_S22_ 9
#define _UM_MD5_S23_ 14
#define _UM_MD5_S24_ 20
#define _UM_MD5_S31_ 4
#define _UM_MD5_S32_ 11
#define _UM_MD5_S33_ 16
#define _UM_MD5_S34_ 23
#define _UM_MD5_S41_ 6
#define _UM_MD5_S42_ 10
#define _UM_MD5_S43_ 15
#define _UM_MD5_S44_ 21


typedef struct {
  um_md5_uint4 state[4];                                   /* state (ABCD) */
  um_md5_uint4 count[2];        /* number of bits, modulo 2^64 (lsb first) */
  unsigned char buffer[64];                         /* input buffer */
} UM_MD5_CTX;

static unsigned char _UM_MD5_PADDING_[64] = {
  0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

/* _UM_MD5_F_, G, H and I are basic MD5 functions.
 */
#define _UM_MD5_F_(x, y, z) (((x) & (y)) | ((~x) & (z)))
#define _UM_MD5_G_(x, y, z) (((x) & (z)) | ((y) & (~z)))
#define _UM_MD5_H_(x, y, z) ((x) ^ (y) ^ (z))
#define _UM_MD5_I_(x, y, z) ((y) ^ ((x) | (~z)))

/* _UM_MD5_ROTATE_LEFT_ rotates x left n bits.
 */
#define _UM_MD5_ROTATE_LEFT_(x, n) (((x) << (n)) | ((x) >> (32-(n))))

/* _UM_MD5_FF_, _UM_MD5_GG_, _UM_MD5_HH_, and _UM_MD5_II_ transformations for rounds 1, 2, 3, and 4.
Rotation is separate from addition to prevent recomputation.
 */
#define _UM_MD5_FF_(a, b, c, d, x, s, ac) { \
 (a) += _UM_MD5_F_ ((b), (c), (d)) + (x) + (um_md5_uint4)(ac); \
 (a) = _UM_MD5_ROTATE_LEFT_ ((a), (s)); \
 (a) += (b); \
  }
#define _UM_MD5_GG_(a, b, c, d, x, s, ac) { \
 (a) += _UM_MD5_G_ ((b), (c), (d)) + (x) + (um_md5_uint4)(ac); \
 (a) = _UM_MD5_ROTATE_LEFT_ ((a), (s)); \
 (a) += (b); \
  }
#define _UM_MD5_HH_(a, b, c, d, x, s, ac) { \
 (a) += _UM_MD5_H_ ((b), (c), (d)) + (x) + (um_md5_uint4)(ac); \
 (a) = _UM_MD5_ROTATE_LEFT_ ((a), (s)); \
 (a) += (b); \
  }
#define _UM_MD5_II_(a, b, c, d, x, s, ac) { \
 (a) += _UM_MD5_I_ ((b), (c), (d)) + (x) + (um_md5_uint4)(ac); \
 (a) = _UM_MD5_ROTATE_LEFT_ ((a), (s)); \
 (a) += (b); \
  }


/* Encodes input (um_md5_uint4) into output (unsigned char). Assumes len is
  a multiple of 4.
 */
static void um_Encode (unsigned char *output,
um_md5_uint4 *input,
unsigned int len)
 
{
  unsigned int i, j;

  for (i = 0, j = 0; j < len; i++, j += 4) {
 output[j] = (unsigned char)(input[i] & 0xff);
 output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
 output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
 output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
  }
}

/* Decodes input (unsigned char) into output (um_md5_uint4). Assumes len is
  a multiple of 4.
 */
static void um_Decode (um_md5_uint4 *output,
unsigned char *input,
unsigned int len)
 
{
  unsigned int i, j;

  for (i = 0, j = 0; j < len; i++, j += 4)
 output[i] = ((um_md5_uint4)input[j]) | (((um_md5_uint4)input[j+1]) << 8) |
   (((um_md5_uint4)input[j+2]) << 16) | (((um_md5_uint4)input[j+3]) << 24);
}

/* Note: Replace "for loop" with standard memcpy if possible.
 */

static void um_MD5_memcpy (um_md5_pointer output,
um_md5_pointer input,
unsigned int len)
 
{
  unsigned int i;

  for (i = 0; i < len; i++)
  output[i] = input[i];
}

/* Note: Replace "for loop" with standard memset if possible.
 */
static void um_MD5_memset (um_md5_pointer output,
int value,
unsigned int len)
 
{
  unsigned int i;

  for (i = 0; i < len; i++)
 ((char *)output)[i] = (char)value;
}


/* MD5 basic transformation. Transforms state based on block.
 */
void um_MD5Transform (um_md5_uint4 state[4],
unsigned char block[64])
 
{
int i=0;
 
 um_md5_uint4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];

  um_Decode (x, block, 64);

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

 /* Round 2 */
  _UM_MD5_GG_ (a, b, c, d, x[ 1], _UM_MD5_S21_, 0xf61e2562); /* 17 */
  _UM_MD5_GG_ (d, a, b, c, x[ 6], _UM_MD5_S22_, 0xc040b340); /* 18 */
  _UM_MD5_GG_ (c, d, a, b, x[11], _UM_MD5_S23_, 0x265e5a51); /* 19 */
  _UM_MD5_GG_ (b, c, d, a, x[ 0], _UM_MD5_S24_, 0xe9b6c7aa); /* 20 */
  _UM_MD5_GG_ (a, b, c, d, x[ 5], _UM_MD5_S21_, 0xd62f105d); /* 21 */
  _UM_MD5_GG_ (d, a, b, c, x[10], _UM_MD5_S22_,  0x2441453); /* 22 */
  _UM_MD5_GG_ (c, d, a, b, x[15], _UM_MD5_S23_, 0xd8a1e681); /* 23 */
  _UM_MD5_GG_ (b, c, d, a, x[ 4], _UM_MD5_S24_, 0xe7d3fbc8); /* 24 */
  _UM_MD5_GG_ (a, b, c, d, x[ 9], _UM_MD5_S21_, 0x21e1cde6); /* 25 */
  _UM_MD5_GG_ (d, a, b, c, x[14], _UM_MD5_S22_, 0xc33707d6); /* 26 */
  _UM_MD5_GG_ (c, d, a, b, x[ 3], _UM_MD5_S23_, 0xf4d50d87); /* 27 */
  _UM_MD5_GG_ (b, c, d, a, x[ 8], _UM_MD5_S24_, 0x455a14ed); /* 28 */
  _UM_MD5_GG_ (a, b, c, d, x[13], _UM_MD5_S21_, 0xa9e3e905); /* 29 */
  _UM_MD5_GG_ (d, a, b, c, x[ 2], _UM_MD5_S22_, 0xfcefa3f8); /* 30 */
  _UM_MD5_GG_ (c, d, a, b, x[ 7], _UM_MD5_S23_, 0x676f02d9); /* 31 */
  _UM_MD5_GG_ (b, c, d, a, x[12], _UM_MD5_S24_, 0x8d2a4c8a); /* 32 */

  /* Round 3 */
  _UM_MD5_HH_ (a, b, c, d, x[ 5], _UM_MD5_S31_, 0xfffa3942); /* 33 */
  _UM_MD5_HH_ (d, a, b, c, x[ 8], _UM_MD5_S32_, 0x8771f681); /* 34 */
  _UM_MD5_HH_ (c, d, a, b, x[11], _UM_MD5_S33_, 0x6d9d6122); /* 35 */
  _UM_MD5_HH_ (b, c, d, a, x[14], _UM_MD5_S34_, 0xfde5380c); /* 36 */
  _UM_MD5_HH_ (a, b, c, d, x[ 1], _UM_MD5_S31_, 0xa4beea44); /* 37 */
  _UM_MD5_HH_ (d, a, b, c, x[ 4], _UM_MD5_S32_, 0x4bdecfa9); /* 38 */
  _UM_MD5_HH_ (c, d, a, b, x[ 7], _UM_MD5_S33_, 0xf6bb4b60); /* 39 */
  _UM_MD5_HH_ (b, c, d, a, x[10], _UM_MD5_S34_, 0xbebfbc70); /* 40 */
  _UM_MD5_HH_ (a, b, c, d, x[13], _UM_MD5_S31_, 0x289b7ec6); /* 41 */
  _UM_MD5_HH_ (d, a, b, c, x[ 0], _UM_MD5_S32_, 0xeaa127fa); /* 42 */
  _UM_MD5_HH_ (c, d, a, b, x[ 3], _UM_MD5_S33_, 0xd4ef3085); /* 43 */
  _UM_MD5_HH_ (b, c, d, a, x[ 6], _UM_MD5_S34_,  0x4881d05); /* 44 */
  _UM_MD5_HH_ (a, b, c, d, x[ 9], _UM_MD5_S31_, 0xd9d4d039); /* 45 */
  _UM_MD5_HH_ (d, a, b, c, x[12], _UM_MD5_S32_, 0xe6db99e5); /* 46 */
  _UM_MD5_HH_ (c, d, a, b, x[15], _UM_MD5_S33_, 0x1fa27cf8); /* 47 */
  _UM_MD5_HH_ (b, c, d, a, x[ 2], _UM_MD5_S34_, 0xc4ac5665); /* 48 */

  /* Round 4 */
  _UM_MD5_II_ (a, b, c, d, x[ 0], _UM_MD5_S41_, 0xf4292244); /* 49 */
  _UM_MD5_II_ (d, a, b, c, x[ 7], _UM_MD5_S42_, 0x432aff97); /* 50 */
  _UM_MD5_II_ (c, d, a, b, x[14], _UM_MD5_S43_, 0xab9423a7); /* 51 */
  _UM_MD5_II_ (b, c, d, a, x[ 5], _UM_MD5_S44_, 0xfc93a039); /* 52 */
  _UM_MD5_II_ (a, b, c, d, x[12], _UM_MD5_S41_, 0x655b59c3); /* 53 */
  _UM_MD5_II_ (d, a, b, c, x[ 3], _UM_MD5_S42_, 0x8f0ccc92); /* 54 */
  _UM_MD5_II_ (c, d, a, b, x[10], _UM_MD5_S43_, 0xffeff47d); /* 55 */
  _UM_MD5_II_ (b, c, d, a, x[ 1], _UM_MD5_S44_, 0x85845dd1); /* 56 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -