A class encapsulated based on others' MD5 source code

Source: Internet
Author: User

// Start the content of the md5.h header file

# Pragma once

# Define uchar unsigned char
Typedef unsigned char * pointer;
Typedef unsigned short int uint2;
Typedef unsigned long int uint4;
# Define proto_list (list) List

// MD5 context.
Typedef struct md5_ctx {
Uint4 State [4]; // state (ABCD)
Uint4 count [2]; // number of BITs, modulo 2 ^ 64 (LSB first)
Unsigned char buffer [64]; // input buffer
} Md5_ctx;

Void md5init proto_list (md5_ctx *));
Void md5update proto_list
(Md5_ctx *, unsigned char *, unsigned INT ));
Void md5final proto_list (unsigned char [16], md5_ctx *));

/* 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

Static void md5transform proto_list (uint4 [4], unsigned char [64]);
Static void encode proto_list
(Unsigned char *, uint4 *, unsigned INT ));
Static void decode proto_list
(Uint4 *, unsigned char *, unsigned INT ));
Static void md5_memcpy proto_list (pointer, pointer, unsigned INT ));
Static void md5_memset proto_list (pointer, Int, unsigned INT ));

Static unsigned char 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
};

/* 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) + (uint4) (AC );/
(A) = rotate_left (a), (s ));/
(A) + = (B );/
}
# Define Gg (a, B, c, d, X, S, AC ){/
(A) + = g (B), (c), (d) + (x) + (uint4) (AC );/
(A) = rotate_left (a), (s ));/
(A) + = (B );/
}
# Define HH (a, B, c, d, X, S, AC ){/
(A) + = H (B), (c), (d) + (x) + (uint4) (AC );/
(A) = rotate_left (a), (s ));/
(A) + = (B );/
}
# Define II (a, B, c, d, X, S, AC ){/
(A) + = I (B), (c), (d) + (x) + (uint4) (AC );/
(A) = rotate_left (a), (s ));/
(A) + = (B );/
}

Class Limit 5
{
PRIVATE:
Void md5update (md5_ctx * context, unsigned char * input, unsigned int inputlen );
Void md5final (unsigned char digest [16], md5_ctx * context );
Static void md5transform (uint4 State [4], unsigned char block [64]);
Void tohex (byte * Out, byte * lpbuffer, DWORD dwsize );
Public:
Required 5 (void );
~ Required 5 (void );
Void mdstring (uchar * string, uchar * Out, int Len );
};

// Required parameters
# Define test_block_len 1000
# Define test_block_count 1000
Static void mdstring proto_list (char *));
Static void mdtimetrial proto_list (void ));
Static void mdtestsuite proto_list (void ));
Static void mdfile proto_list (char *));
Static void mdfilter proto_list (void ));
Static void mdprint proto_list (unsigned char [16]);
# Define md5_ctx md5_ctx
# Define mdinit md5init
# Define mdupdate md5update
# Define mdfinal md5final
/* Digests a string and prints the result.
*/

// End the md5.h File

// Start the md5.cap File Content

# Include "stdafx. H"
# Include "./md5.h"

Authorization 5: Authorization 5 (void)
{
}

Limit 5 ::~ Authorization 5 (void)
{
}

/* MD5 initialization. begins an MD5 operation, writing a new context.
*/
Void md5init (md5_ctx * 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
Context.
*/
Void limit 5: md5update (md5_ctx * context, unsigned char * input, unsigned int inputlen)
{
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] + = (uint4) inputlen <3 ))
<(Uint4) inputlen <3 ))
Context-> count [1] ++;
Context-> count [1] + = (uint4) inputlen> 29 );

Partlen = 64-index;

/* Transform as same times as possible.
*/
If (inputlen> = partlen ){
Md5_memcpy
(Pointer) & context-> buffer [Index], (pointer) input, partlen );
Md5transform (context-> state, context-> buffer );

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

Index = 0;
}
Else
I = 0;

/* Buffer remaining input */
Md5_memcpy
(Pointer) & context-> buffer [Index], (pointer) & input [I],
Inputlen-I );
}

/* MD5 finalization. ends an MD5 message-digest operation, writing
The message digest and zeroizing the context.
*/
Void limit 5: md5final (unsigned char digest [16], md5_ctx * context)
{
Unsigned char bits [8];
Unsigned int index, padlen;

/* Save Number of BITs */
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 );
Md5update (context, padding, padlen );

/* Append length (before padding )*/
Md5update (context, bits, 8 );

/* Store state in Digest */
Encode (Digest, context-> state, 16 );

/* Zeroize sensitive information.
*/
Md5_memset (pointer) Context, 0, sizeof (* context ));
}

/* MD5 basic transformation. transforms state based on block.
*/
Static void encode (unsigned char * output, 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 (uint4). Assumes Len is
A multiple of 4.
*/
Static void decode (uint4 * output, unsigned char * input, unsigned int Len)
{
Unsigned int I, J;

For (I = 0, j = 0; j <Len; I ++, J + = 4)
Output [I] = (uint4) input [J]) | (uint4) input [J + 1]) <8) |
(Uint4) input [J + 2]) <16) | (uint4) input [J + 3]) <24 );
}

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

Static void md5_memcpy (pointer output, 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 md5_memset (pointer output, int value, unsigned int Len)
{
Unsigned int I;

For (I = 0; I <Len; I ++)
(Char *) Output) [I] = (char) value;
}

Void upload 5: md5transform (uint4 State [4], unsigned char block [64])
{
Uint4 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 (pointer) x, 0, sizeof (x ));
}

/* Encodes input (uint4) into output (unsigned char). Assumes Len is
A multiple of 4.
*/
Void limit 5: mdstring (uchar * string, uchar * Out, int Len)
{
// Uchar BB [10], I;
Md5_ctx context;
// Ansistring S;

Unsigned char digest [16];
// Unsigned int Len = strlen (char *) string );

Memset (Digest, 0, sizeof (Digest ));
Mdinit (& context );
Mdupdate (& context, String, Len );
Mdfinal (Digest, & context );
Tohex (Out, digest, sizeof (Digest ));
}

Void limit 5: tohex (uchar * Out, uchar * lpbuffer, DWORD dwsize)
{
Char TMP [1024] = {0 };
Char T [10] = {0 };
For (INT I = 0; I <(long) dwsize; I ++)
{
Sprintf (T, "% 02x", lpbuffer [I]);
Strcat (TMP, t );
}
Memcpy (Out, TMP, strlen (TMP ));
}

// End the md5.cpp File

Call:

Listen 5 mymd5;

Byte s [256] = {0 };

Byte MD5 [50] = {0 };

Cmymd5.mdstring (MD5, S, sizeof (s ));

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.