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

📄 um_md5.c

📁 hammerOS(了解的人就知道了)的一个板块Manage的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
  _UM_MD5_II_ (a, b, c, d, x[ 8], _UM_MD5_S41_, 0x6fa87e4f); /* 57 */
  _UM_MD5_II_ (d, a, b, c, x[15], _UM_MD5_S42_, 0xfe2ce6e0); /* 58 */
  _UM_MD5_II_ (c, d, a, b, x[ 6], _UM_MD5_S43_, 0xa3014314); /* 59 */
  _UM_MD5_II_ (b, c, d, a, x[13], _UM_MD5_S44_, 0x4e0811a1); /* 60 */
  _UM_MD5_II_ (a, b, c, d, x[ 4], _UM_MD5_S41_, 0xf7537e82); /* 61 */
  _UM_MD5_II_ (d, a, b, c, x[11], _UM_MD5_S42_, 0xbd3af235); /* 62 */
  _UM_MD5_II_ (c, d, a, b, x[ 2], _UM_MD5_S43_, 0x2ad7d2bb); /* 63 */
  _UM_MD5_II_ (b, c, d, a, x[ 9], _UM_MD5_S44_, 0xeb86d391); /* 64 */

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

  /* Zeroize sensitive information.
  */
  um_MD5_memset ((um_md5_pointer)x, 0, sizeof (x));
}



/* MD5 initialization. Begins an MD5 operation, writing a new context.
 */
void um_MD5_init (UM_MD5_CTX *context)
                                        /* context */
{
  context->count[0] = context->count[1] = 0;
  /* Load magic initialization constants.
*/
  context->state[0] = 0x67452301;
  context->state[1] = 0xefcdab89;
  context->state[2] = 0x98badcfe;
  context->state[3] = 0x10325476;
}

/* MD5 block update operation. Continues an MD5 message-digest
  operation, processing another message block, and updating the
  context.
 */
void um_MD5Update (UM_MD5_CTX *context, unsigned char *input,unsigned int inputLen  )
                                         /* context */
                              /* input block */
                     /* length of input block */
{
  unsigned int i, index, partLen;

  /* Compute number of bytes mod 64 */
  index = (unsigned int)((context->count[0] >> 3) & 0x3F);

  /* Update number of bits */
  if ((context->count[0] += ((um_md5_uint4)inputLen << 3))
  < ((um_md5_uint4)inputLen << 3))
 context->count[1]++;
  context->count[1] += ((um_md5_uint4)inputLen >> 29);

  partLen = 64 - index;

  /* Transform as many times as possible.
*/
  if (inputLen >= partLen) {
 um_MD5_memcpy
   ((um_md5_pointer)&context->buffer[index], (um_md5_pointer)input, partLen);
 um_MD5Transform (context->state, context->buffer);

 for (i = partLen; i + 63 < inputLen; i += 64)
   um_MD5Transform (context->state, &input[i]);

 index = 0;
  }
  else
 i = 0;

  /* Buffer remaining input */
  um_MD5_memcpy
 ((um_md5_pointer)&context->buffer[index], (um_md5_pointer)&input[i],
  inputLen-i);
}

/* MD5 finalization. Ends an MD5 message-digest operation, writing the
  the message digest and zeroizing the context.
 */
void um_MD5Final (unsigned char digest[16], UM_MD5_CTX *context)
                       /* message digest */
                                        /* context */
{
  unsigned char bits[8];
  unsigned int index, padLen;

  /* Save number of bits */
  um_Encode (bits, context->count, 8);

  /* Pad out to 56 mod 64.
*/
  index = (unsigned int)((context->count[0] >> 3) & 0x3f);
  padLen = (index < 56) ? (56 - index) : (120 - index);
  um_MD5Update (context,(unsigned char*) _UM_MD5_PADDING_, padLen);

  /* Append length (before _UM_MD5_PADDING_) */
  um_MD5Update (context, bits, 8);
  /* Store state in digest */
  um_Encode (digest, context->state, 16);

  /* Zeroize sensitive information.
*/
  um_MD5_memset ((um_md5_pointer)context, 0, sizeof (*context));
}


  
/* Digests a file and prints the result.
 */
char* um_MDFile (char *filename)
 
{ static char output[33]={"\0"};
  FILE *file;
  UM_MD5_CTX context;
  int len;
  unsigned char buffer[1024], digest[16];
  int i;
  char output1[32];
  if ((file = fopen (filename, "rb")) == NULL)
   { printf ("%s can't be opened\n", filename);
    return 0;
   }
  else {
   	um_MD5_init (&context);
 	while (len = fread (buffer, 1, 1024, file))
   	um_MD5Update (&context, buffer, len);
	um_MD5Final (digest, &context);
	fclose (file);
	for (i = 0; i < 16; i++)
	 {sprintf(&(output1[2*i]),"%02x",(unsigned char)digest[i]);
  	  sprintf(&(output1[2*i+1]),"%02x",(unsigned char)(digest[i]<<4));
  		}
        for(i=0;i<32;i++)
       output[i]=output1[i];
        return output;
       }
}




/* Digests a string and prints the result.
 */
char* um_MDString (char *string)
 
{
  UM_MD5_CTX context;
  unsigned char digest[16];
  char output1[32];
 static  char output[33]={"\0"};
  unsigned int len = strlen (string);
  int i;
  um_MD5_init (&context);
  um_MD5Update (&context, (unsigned char*)string, len);
  um_MD5Final (digest, &context);

  for (i = 0; i < 16; i++)
 {sprintf(&(output1[2*i]),"%02x",(unsigned char)digest[i]);
  sprintf(&(output1[2*i+1]),"%02x",(unsigned char)(digest[i]<<4));
  }
  for(i=0;i<32;i++)
  output[i]=output1[i];
  return output;
}


char* um_md5_key(char* text,char*  key)
{
        char   digest[16];
        char   output1[32];
         static char output[33]={"\0"};
        UM_MD5_CTX context;
        unsigned char k_ipad[65];    /* inner _UM_MD5_PADDING_ -
                                      * key XORd with ipad
                                      */
        unsigned char k_opad[65];    /* outer _UM_MD5_PADDING_ -
                                      * key XORd with opad
                                      */
        unsigned char tk[16];
        int i;
        int text_len = strlen (text);
        int key_len=strlen(key);
        /* if key is longer than 64 bytes reset it to key=MD5(key) */
        if (key_len > 64) {

                UM_MD5_CTX      tctx;

                um_MD5_init(&tctx);
                um_MD5Update(&tctx,(unsigned char*) key, key_len);
                um_MD5Final(tk, &tctx);

                key = (char*)tk;
                key_len = 16;
        }

        /*
         * the HMAC_MD5 transform looks like:
         *
         * MD5(K XOR opad, MD5(K XOR ipad, text))
         *
         * where K is an n byte key
         * ipad is the byte 0x36 repeated 64 times
         * opad is the byte 0x5c repeated 64 times
         * and text is the data being protected
         */

        /* start out by storing key in pads */
        
        /*bzero( k_ipad, sizeof k_ipad);
          bzero( k_opad, sizeof k_opad);
        */

        for(i=0;i<65;i++)
        k_ipad[i]=(unsigned char)0;
        for(i=0;i<65;i++)
        k_opad[i]=(unsigned char)0;

        /*bcopy( key, k_ipad, key_len);
          bcopy( key, k_opad, key_len);
         */
         for(i=0;i<key_len;i++)
        {k_ipad[i]=(unsigned char)key[i];
         k_opad[i]=(unsigned char)key[i];
         }

        /* XOR key with ipad and opad values */
        for (i=0; i<64; i++) {
                k_ipad[i] ^= 0x36;
                k_opad[i] ^= 0x5c;
        }
        /*
         * perform inner MD5
         */
        um_MD5_init(&context);                   /* init context for 1st
                                              * pass */
        um_MD5Update(&context, k_ipad, 64);      /* start with inner pad */
        um_MD5Update(&context, (unsigned char*)text, text_len); /* then text of datagram 

*/
        um_MD5Final((unsigned char*)digest, &context);          /* finish up 1st pass */
        /*
         * perform outer MD5
         */
        um_MD5_init(&context);                   /* init context for 2nd
                                              * pass */
        um_MD5Update(&context, k_opad, 64);     /* start with outer pad */
        um_MD5Update(&context,(unsigned char*) digest, 16);     /* then results of 1st
                                              * hash */
        um_MD5Final((unsigned char*)digest, &context);          /* finish up 2nd pass */
        for (i = 0; i < 16; i++)
        {sprintf(&(output1[2*i]),"%02x",(unsigned char)digest[i]);
         sprintf(&(output1[2*i+1]),"%02x",(unsigned char)(digest[i]<<4));
          }
        for(i=0;i<32;i++)
        output[i]=output1[i]; 
        return output;     
}

 #ifdef _cplusplus
}
#endif

⌨️ 快捷键说明

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