C ++ MD5 source file

Source: Internet
Author: User
Posting time: 13:22:00

/*************************************** ***************************************
* Copyright (c) 2000 by Robert Hubley .*
* All Rights Reserved .*
**
* This software is provided ''as is ''and any express or implied *
* Warranties, including, but not limited to, the implied warranties *
* Merchantability and fitness for a particle purpose, are disclaimed .*
* In no event shall the authors be liable for any direct, indirect ,*
* Incidental, special, exemplary, or consequential damages (including, *
* Not limited to, procurement of substitute goods or services; loss of use ,*
* Data, or profits; or business interruption) however caused and on any *
* Theory of liability, whether in contract, strict liability, or tort *
* (Including negligence or otherwise) arising in any way out of the use *
* This software, even if advised of the possibility of such damage .*
**
**************************************** **************************************

Md5c. C-RSA Data Security, inc., MD5 message-Digest algorithm

Port to Win32 DLL by Robert Hubley 1/5/2000

Original copyright:

Copyright (c) 1991-2, RSA Data Security, Inc. Created 1991. All
Rights Reserved.

License to copy and use this software is granted provided that it
Is identified as the "RSA Data Security, Inc. MD5 message-Digest
Algorithm "in all material mentioning or referencing this software
Or this function.

License is also granted to make and use derivative works provided
That such works are identified as "derived from the RSA Data
Security, Inc. MD5 message-Digest algorithm "in all material
Mentioning or referencing the derived work.
 
RSA Data Security, Inc. makes no representations concerning either
The merchantability of this software or the suitability of this
Software for any special 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.
*/

# Include "md5.h"

/* Constants for md5transform routine.
*/
# 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

/* F, G, H and I are basic MD5 functions.
*/
# Define f (x, y, z) (x) & (y) | ((~ X) & (z )))
# Define g (x, y, z) (x) & (z) | (y )&(~ Z )))
# Define h (x, y, z) (x) ^ (y) ^ (z ))
# Define I (x, y, z) (y) ^ (x) | (~ Z )))

/* Rotate_left rotates x left N bits.
*/
# Define rotate_left (x, n) (x) <(N) | (x)> (32-(N ))))

/* Ff, GG, HH, and II transformations for Rounds 1, 2, 3, and 4.
Rotation is separate from addition to prevent recomputation.
*/
# Define ff (a, B, c, d, X, S, AC ){"
(A) + = f (B), (c), (d) + (x) + (unsigned long INT) (AC );"
(A) = rotate_left (a), (s ));"
(A) + = (B );"
}
# Define Gg (a, B, c, d, X, S, AC ){"
(A) + = g (B), (c), (d) + (x) + (unsigned long INT) (AC );"
(A) = rotate_left (a), (s ));"
(A) + = (B );"
}
# Define HH (a, B, c, d, X, S, AC ){"
(A) + = H (B), (c), (d) + (x) + (unsigned long INT) (AC );"
(A) = rotate_left (a), (s ));"
(A) + = (B );"
}
# Define II (a, B, c, d, X, S, AC ){"
(A) + = I (B), (c), (d) + (x) + (unsigned long INT) (AC );"
(A) = rotate_left (a), (s ));"
(A) + = (B );"
}

/* MD5 initialization. begins an MD5 operation, writing a new context.
*/

Md5_ctx: md5_ctx ()
{
Md5init ();
}

Md5_ctx ::~ Md5_ctx ()
{
}

Void md5_ctx: md5init ()
{
This-> count [0] = This-> count [1] = 0;
/* Load magic initialization constants .*/
This-> State [0] = 0x67452301;
This-> State [1] = 0xefcdab89;
This-> State [2] = 0x98badcfe;
This-> State [3] = 0x10325476;
/* Add By liguangyi */
Md5_memset (padding, 0, sizeof (padding ));
* Padding = 0x80;
// Padding = {
// 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, 0, 0, 0 };
}

/* MD5 block update operation. continues an MD5 message-Digest
Operation, processing another message block, and updating
Context.
*/
Void md5_ctx: md5update (unsigned char * input, unsigned int inputlen)
{
Unsigned int I, index, partlen;
 
/* Compute number of bytes mod 64 */
Index = (unsigned INT) (this-> count [0]> 3) & 0x3f );
 
/* UPDATE Number of BITs */
If (this-> count [0] + = (unsigned long INT) inputlen <3 ))
<(Unsigned long INT) inputlen <3 ))
This-> count [1] ++;
This-> count [1] + = (unsigned long INT) inputlen> 29 );
 
Partlen = 64-index;
 
/* Transform as same times as possible.
*/
If (inputlen> = partlen ){
Md5_memcpy (unsigned char *) & this-> buffer [Index],
(Unsigned char *) input, partlen );
Md5transform (this-> state, this-> buffer );

For (I = partlen; I + 63 <inputlen; I ++ = 64)
Md5transform (this-> state, & input [I]);

Index = 0;
}
Else
I = 0;
 
/* Buffer remaining input */
Md5_memcpy (unsigned char *) & this-> buffer [Index], (unsigned char *) & input [I], inputlen-I );
}

/* MD5 finalization. ends an MD5 message-digest operation, writing
The message digest and zeroizing the context.
*/
Void md5_ctx: md5final (unsigned char digest [16])
{
Unsigned char bits [8];
Unsigned int index, padlen;
 
/* Save Number of BITs */
Encode (bits, this-> count, 8 );
 
/* Pad out to 56 mod 64.
*/
Index = (unsigned INT) (this-> count [0]> 3) & 0x3f );
Padlen = (index <56 )? (56-index): (120-index );
Md5update (padding, padlen );
 
/* Append length (before padding )*/
Md5update (bits, 8 );
/* Store state in Digest */
Encode (Digest, this-> state, 16 );
 
/* Zeroize sensitive information.
*/
Md5_memset (unsigned char *) This, 0, sizeof (* This ));
This-> md5init ();
}

/* MD5 basic transformation. transforms state based on block.
*/
Void md5_ctx: md5transform (unsigned long int State [4], unsigned char block [64])
{
Unsigned long int A = State [0], B = State [1], c = State [2], D = State [3], X [16];
 
Decode (x, block, 64 );
 
/* Round 1 */
Ff (a, B, c, d, X [0], S11, 0xd76aa478);/* 1 */
Ff (D, a, B, c, X [1], S12, 0xe8c7b756);/* 2 */
Ff (c, d, A, B, X [2], S13, 0x242070db);/* 3 */
Ff (B, c, d, A, X [3], S14, 0xc1bdceee);/* 4 */
Ff (a, B, c, d, X [4], S11, 0xf57c0faf);/* 5 */
Ff (D, a, B, c, X [5], S12, 0x4787c62a);/* 6 */
Ff (c, d, A, B, X [6], S13, 0xa8304613);/* 7 */
Ff (B, c, d, A, X [7], S14, 0xfd469501);/* 8 */
Ff (a, B, c, d, X [8], S11, 0x698098d8);/* 9 */
Ff (D, a, B, c, X [9], S12, 0x8b44f7af);/* 10 */
Ff (c, d, A, B, X [10], S13, 0xffff5bb1);/* 11 */
Ff (B, c, d, A, X [11], S14, 0x895cd7be);/* 12 */
Ff (a, B, c, d, X [12], S11, 0x6b901122);/* 13 */
Ff (D, a, B, c, X [13], S12, 0xfd987193);/* 14 */
Ff (c, d, A, B, X [14], S13, 0xa679438e);/* 15 */
Ff (B, c, d, A, X [15], S14, 0x49b40821);/* 16 */
 
/* Round 2 */
GG (a, B, c, d, X [1], S21, 0xf61e2562);/* 17 */
GG (D, a, B, c, X [6], s22, 0xc040b340);/* 18 */
GG (c, d, A, B, X [11], S23, 0x265e5a51);/* 19 */
GG (B, c, d, A, X [0], S24, 0xe9b6c7aa);/* 20 */
GG (a, B, c, d, X [5], S21, 0xd62f105d);/* 21 */
GG (D, a, B, c, X [10], s22, 0x2441453);/* 22 */
GG (c, d, A, B, X [15], S23, 0xd8a1e681);/* 23 */
GG (B, c, d, A, X [4], S24, 0xe7d3fbc8);/* 24 */
GG (a, B, c, d, X [9], S21, 0x21e1cde6);/* 25 */
GG (D, a, B, c, X [14], s22, 0xc33707d6);/* 26 */
GG (c, d, A, B, X [3], S23, 0xf4d50d87);/* 27 */
GG (B, c, d, A, X [8], S24, 0x455a14ed);/* 28 */
GG (a, B, c, d, X [13], S21, 0xa9e3e905);/* 29 */
GG (D, a, B, c, X [2], s22, 0xfcefa3f8);/* 30 */
GG (c, d, A, B, X [7], S23, 0x676f02d9);/* 31 */
GG (B, c, d, A, X [12], S24, 0x8d2a4c8a);/* 32 */
 
/* Round 3 */
HH (a, B, c, d, X [5], s31, 0xfffa3942);/* 33 */
HH (D, a, B, c, X [8], s32, 0x8771f681);/* 34 */
HH (c, d, A, B, X [11], s33, 0x6d9d6122);/* 35 */
HH (B, c, d, A, X [14], s34, 0xfde5380c);/* 36 */
HH (a, B, c, d, X [1], s31, 0xa4beea44);/* 37 */
HH (D, a, B, c, X [4], s32, 0x4bdecfa9);/* 38 */
HH (c, d, A, B, X [7], s33, 0xf6bb4b60);/* 39 */
HH (B, c, d, A, X [10], s34, 0xbebfbc70);/* 40 */
HH (a, B, c, d, X [13], s31, 0x289b7ec6);/* 41 */
HH (D, a, B, c, X [0], s32, 0xeaa1_fa);/* 42 */
HH (c, d, A, B, X [3], s33, 0xd4ef3085);/* 43 */
HH (B, c, d, A, X [6], s34, 0x4881d05);/* 44 */
HH (a, B, c, d, X [9], s31, 0xd9d4d039);/* 45 */
HH (D, a, B, c, X [12], s32, 0xe6db99e5);/* 46 */
HH (c, d, A, B, X [15], s33, 0x1fa27cf8);/* 47 */
HH (B, c, d, A, X [2], s34, 0xc4ac5665);/* 48 */
 
/* Round 4 */
II (a, B, c, d, X [0], s41, 0xf4292244);/* 49 */
II (D, a, B, c, X [7], S42, 0x432aff97);/* 50 */
II (c, d, A, B, X [14], s43, 0xab9423a7);/* 51 */
II (B, c, d, A, X [5], s44, 0xfc93a039);/* 52 */
II (a, B, c, d, X [12], s41, 0x655b59c3);/* 53 */
II (D, a, B, c, X [3], S42, 0x8f0ccc92);/* 54 */
II (c, d, A, B, X [10], s43, 0xffeff47d);/* 55 */
II (B, c, d, A, X [1], s44, 0x85845dd1);/* 56 */
II (a, B, c, d, X [8], s41, 0x6fa87e4f);/* 57 */
II (D, a, B, c, X [15], S42, 0xfe2ce6e0);/* 58 */
II (c, d, A, B, X [6], s43, 0xa3014314);/* 59 */
II (B, c, d, A, X [13], s44, 0x4e0811a1);/* 60 */
II (a, B, c, d, X [4], s41, 0xf7537e82);/* 61 */
II (D, a, B, c, X [11], S42, 0xbd3af235);/* 62 */
II (c, d, A, B, X [2], s43, 0x2ad7d2bb);/* 63 */
II (B, c, d, A, X [9], s44, 0xeb86d391);/* 64 */
 
State [0] + =;
State [1] + = B;
State [2] + = C;
State [3] + = D;
 
/* Zeroize sensitive information.
*/
Md5_memset (unsigned char *) x, 0, sizeof (x ));
}

/* Encodes input (unsigned long INT) into output (unsigned char). Assumes Len is
A multiple of 4.
*/
Void md5_ctx: encode (unsigned char * output, unsigned long int * 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 (unsigned long INT). Assumes Len is
A multiple of 4.
*/
Void md5_ctx: Decode (unsigned long int * output, unsigned char * input, unsigned int Len)
{
Unsigned int I, J;
 
For (I = 0, j = 0; j <Len; I ++, J + = 4)
Output [I] = (unsigned long INT) input [J]) | (unsigned long INT) input [J + 1]) <8) |
(Unsigned long INT) input [J + 2]) <16) | (unsigned long INT) input [J + 3]) <24 );
}

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

Void md5_ctx: md5_memcpy (unsigned char * output, unsigned char * 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.
*/
Void md5_ctx: md5_memset (unsigned char * output, int value, unsigned int Len)
{
Unsigned int I;
 
For (I = 0; I <Len; I ++)
(Char *) Output) [I] = (char) value;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.