Algorithm series 9 MD5, algorithm 9 md5

Source: Internet
Author: User
Tags rounds

Algorithm series 9 MD5, algorithm 9 md5

 

MD5 is the Message-Digest Algorithm 5 (Information-Digest Algorithm 5), which is used to ensure the integrity and consistency of information transmission. It is one of the widely used Hash Algorithms in computers (also translated digest algorithms and hash algorithms). mainstream programming languages generally have MD5 implementations.

Computation of data (such as Chinese characters) into another fixed length value is the basic principle of the hash algorithm. The predecessor of MD5 is MD2, MD3, and MD4.

The function of MD5 is to compress large-capacity information into a confidential format before signing a private key using digital signature software (that is, to convert a byte string of any length into a certain length of sixteen number string ).



A brief description of the MD5 algorithm can be: MD5 processes input information in 512-bit groups, and each group is divided into 16 32-seat groups, after a series of processing, the algorithm output consists of four 32-bit groups. After these four 32-bit groups are cascade, a 128-bit hash value is generated.
In the MD5 algorithm, you first need to fill in the information so that the bitwise length is equal to 512 for the result of 448 remainder. Therefore, the Bits Length is extended to N * 512 + 448, N is a non-negative integer, and N can be zero. The filling method is as follows. Fill in one and countless zeros after the information until the above conditions are met. Then, append a length of the prefill information in 64-bit binary format to the result. After the two steps, the bit length of the information is N * 512 + 448 + 64 = (N + 1) * 512, that is, the length is exactly an integer multiple of 512. The reason for this is to meet the requirements for the length of information in subsequent processing. The overall process is shown in. It indicates the I-th group. Each operation is performed by the first 128-bit result value and the 512bit value of the I-th block. The initial 128-bit value is the initial link variable. These parameters are used for the first round of operation, expressed in the upper-byte order, respectively: A = 0x01234567, B = 0x89ABCDEF, C = 0xFEDCBA98, D = 0x76543210.



C ++ code implementation






1. MD5.h


#pragma once/* POINTER defines a generic pointer type */ typedef unsigned char *POINTER; /* UINT2 defines a two byte word */ typedef unsigned short int UINT2; /* UINT4 defines a four byte word */ typedef unsigned long int UINT4; class cMd5{private:    unsigned int m_Content[16];    unsigned int m_ContentLen;    unsigned int m_TotalLen;    static unsigned int m_T[64];    unsigned int m_A, m_B, m_C, m_D;private:    inline void FF(unsigned int& a, unsigned int b, unsigned int c, unsigned int d, int k, int i, int s);    inline void GG(unsigned int& a, unsigned int b, unsigned int c, unsigned int d, int k, int i, int s);    inline void HH(unsigned int& a, unsigned int b, unsigned int c, unsigned int d, int k, int i, int s);    inline void II(unsigned int& a, unsigned int b, unsigned int c, unsigned int d, int k, int i, int s);    inline unsigned int roateLeft(unsigned int a, unsigned int c);    void transform();/* MD5 context. */ typedef struct _MD5_CTX{ UINT4 state[4];                                   /* state (ABCD) */ UINT4 count[2];/* bit ,2^64(Low before) */ unsigned char buffer[64];                         /* input buffer */ } MD5_CTX; void MD5Init (MD5_CTX *); void MD5Update (MD5_CTX *, unsigned char *, unsigned int); void MD5Final (unsigned char [16], MD5_CTX *); void MD5String(char *string, long buflen, char digest[]); static void MD5Transform (UINT4 [4], unsigned char [64]); static void Encode (unsigned char *, UINT4 *, unsigned int); static void Decode (UINT4 *, unsigned char *, unsigned int); static void MD5_memcpy (POINTER, POINTER, unsigned int); static void MD5_memset (POINTER, int, unsigned int); public:cMd5();virtual ~cMd5();    //initialize    void beginCal();    //Show that no longer input data, will add a filling    void endCal();    //Is initialized, input data, in endCal call before, can continue to add data    void addData(unsigned char* data, unsigned int size);    //End after operation, get the code    unsigned char* getCode();    void  clearMemoryHelper(char* p);};

2. MD5.CPP


#include "stdafx.h"#include "Md5.h"#include <string.h>#include <stdio.h>unsigned int cMd5::m_T[64] = {          0xD76aa478, 0xE8C7B756, 0x242070DB, 0xC1BDCEEE,          0xF57C0FAF, 0x4787C62A, 0xA8304613, 0xFD469501,          0x698098D8, 0x8B44F7AF, 0xFFFF5BB1, 0x895CD7BE,          0x6B901122, 0xFD987193, 0xA679438E, 0x49B40821,          0xF61E2562, 0xC040B340, 0x265E5A51, 0xE9B6C7AA,          0xD62F105D, 0x02441453, 0xD8A1E681, 0xE7D3FBC8,          0x21E1CDE6, 0xC33707D6, 0xF4D50D87, 0x455A14ED,          0xA9E3E905, 0xFCEFA3F8, 0x676F02D9, 0x8D2A4C8A,          0xFFFA3942, 0x8771F681, 0x6D9D6122, 0xFDE5380C,          0xA4BEEA44, 0x4BDECFA9, 0xF6BB4B60, 0xBEBFBC70,          0x289B7EC6, 0xEAA127FA, 0xD4EF3085, 0x04881D05,          0xD9D4D039, 0xE6DB99E5, 0x1FA27CF8, 0xC4AC5665,          0xF4292244, 0x432AFF97, 0xAB9423A7, 0xFC93A039,          0x655B59C3, 0x8F0CCC92, 0xFFEFF47D, 0x85845DD1,          0x6FA87E4F, 0xFE2CE6E0, 0xA3014314, 0x4E0811A1,          0xF7537E82, 0xBD3AF235, 0x2AD7D2BB, 0xEB86D391          };cMd5::cMd5(){}cMd5::~cMd5(){}void cMd5::FF(unsigned int& a, unsigned int b, unsigned int c, unsigned int d, int k, int i, int s){    a = b  +  roateLeft(a  +  ((b & c) | ((~b) & d))  +  m_Content[k]  +  m_T[i], s);}void cMd5::GG(unsigned int& a, unsigned int b, unsigned int c, unsigned int d, int k, int i, int s){    a = b  +  roateLeft(a  +  ((b & d) | (c & (~d)))  +  m_Content[k]  +  m_T[i], s);}void cMd5::HH(unsigned int& a, unsigned int b, unsigned int c, unsigned int d, int k, int i, int s){    a = b  +  roateLeft(a  +  (b ^ c ^ d)  +  m_Content[k]  +  m_T[i], s);}void cMd5::II(unsigned int& a, unsigned int b, unsigned int c, unsigned int d, int k, int i, int s){    a = b  +  roateLeft(a  +  (c ^ (b | (~d)))  +  m_Content[k]  +  m_T[i], s);}unsigned int cMd5::roateLeft(unsigned int a, unsigned int c){    return ( (a << c) | (a >> (32-c)) );}void cMd5::transform(){    unsigned int AA, BB, CC, DD;    AA = m_A;    BB = m_B;    CC = m_C;    DD = m_D;    //1 round    FF(AA, BB, CC, DD, 0, 0, 7);    FF(DD, AA, BB, CC, 1, 1, 12);    FF(CC, DD, AA, BB, 2, 2, 17);    FF(BB, CC, DD, AA, 3, 3, 22);    FF(AA, BB, CC, DD, 4, 4, 7);    FF(DD, AA, BB, CC, 5, 5, 12);    FF(CC, DD, AA, BB, 6, 6, 17);    FF(BB, CC, DD, AA, 7, 7, 22);    FF(AA, BB, CC, DD, 8, 8, 7);    FF(DD, AA, BB, CC, 9, 9, 12);    FF(CC, DD, AA, BB, 10, 10, 17);    FF(BB, CC, DD, AA, 11, 11, 22);    FF(AA, BB, CC, DD, 12, 12, 7);    FF(DD, AA, BB, CC, 13, 13, 12);    FF(CC, DD, AA, BB, 14, 14, 17);    FF(BB, CC, DD, AA, 15, 15, 22);    //2 round    GG(AA, BB, CC, DD, 1, 16, 5);    GG(DD, AA, BB, CC, 6, 17, 9);    GG(CC, DD, AA, BB, 11, 18, 14);    GG(BB, CC, DD, AA, 0, 19, 20);    GG(AA, BB, CC, DD, 5, 20, 5);    GG(DD, AA, BB, CC, 10, 21, 9);    GG(CC, DD, AA, BB, 15, 22, 14);    GG(BB, CC, DD, AA, 4, 23, 20);    GG(AA, BB, CC, DD, 9, 24, 5);    GG(DD, AA, BB, CC, 14, 25, 9);    GG(CC, DD, AA, BB, 3, 26, 14);    GG(BB, CC, DD, AA, 8, 27, 20);    GG(AA, BB, CC, DD, 13, 28, 5);    GG(DD, AA, BB, CC, 2, 29, 9);    GG(CC, DD, AA, BB, 7, 30, 14);    GG(BB, CC, DD, AA, 12, 31, 20);    //3 round    HH(AA, BB, CC, DD, 5, 32, 4);    HH(DD, AA, BB, CC, 8, 33, 11);    HH(CC, DD, AA, BB, 11, 34, 16);    HH(BB, CC, DD, AA, 14, 35, 23);    HH(AA, BB, CC, DD, 1, 36, 4);    HH(DD, AA, BB, CC, 4, 37, 11);    HH(CC, DD, AA, BB, 7, 38, 16);    HH(BB, CC, DD, AA, 10, 39, 23);    HH(AA, BB, CC, DD, 13, 40, 4);    HH(DD, AA, BB, CC, 0, 41, 11);    HH(CC, DD, AA, BB, 3, 42, 16);    HH(BB, CC, DD, AA, 6, 43, 23);    HH(AA, BB, CC, DD, 9, 44, 4);    HH(DD, AA, BB, CC, 12, 45, 11);    HH(CC, DD, AA, BB, 15, 46, 16);    HH(BB, CC, DD, AA, 2, 47, 23);    //4 round    II(AA, BB, CC, DD, 0, 48, 6);    II(DD, AA, BB, CC, 7, 49, 10);    II(CC, DD, AA, BB, 14, 50, 15);    II(BB, CC, DD, AA, 5, 51, 21);    II(AA, BB, CC, DD, 12, 52, 6);    II(DD, AA, BB, CC, 3, 53, 10);    II(CC, DD, AA, BB, 10, 54, 15);    II(BB, CC, DD, AA, 1, 55, 21);    II(AA, BB, CC, DD, 8, 56, 6);    II(DD, AA, BB, CC, 15, 57, 10);    II(CC, DD, AA, BB, 6, 58, 15);    II(BB, CC, DD, AA, 13, 59, 21);    II(AA, BB, CC, DD, 4, 60, 6);    II(DD, AA, BB, CC, 11, 61, 10);    II(CC, DD, AA, BB, 2, 62, 15);    II(BB, CC, DD, AA, 9, 63, 21);    m_A = m_A  +  AA;    m_B = m_B  +  BB;    m_C = m_C  +  CC;    m_D = m_D  +  DD;}//initializevoid cMd5::beginCal(){    memset(m_Content, 0, 64);    m_ContentLen = 0;    m_TotalLen = 0;    m_A = 0x67452301;    m_B = 0xEFCDAB89;    m_C = 0x98BADCFE;    m_D = 0x10325476;}void cMd5::endCal(){    memset((char*)m_Content  +  m_ContentLen, 0x80, 1);    if (m_ContentLen < 56){     memset((char*)m_Content  +  m_ContentLen  +  1, 0, 55 - m_ContentLen);    }    else{     memset((char*)m_Content  +  m_ContentLen  +  1, 0, 63 - m_ContentLen);     transform();     memset((char*)m_Content, 0, 56);    }    unsigned int h, l;    h = 0;    l = m_TotalLen;    h |= ((l & 0x80000000) >> 31);    h <<= 1;    l <<= 1;    h |= ((l & 0x80000000) >> 31);    h <<= 1;    l <<= 1;    h |= ((l & 0x80000000) >> 31);    h <<= 1;    l <<= 1;    memcpy(m_Content  +  14, &l, 4);    memcpy(m_Content  +  15, &h, 4);    transform();}void cMd5::addData(unsigned char* data, unsigned int size){    unsigned int i = 0;    unsigned int remain = 64 - m_ContentLen;    if (remain > size)remain = size;    memcpy(((char*)m_Content)  +  m_ContentLen, data, remain);i  += remain;    m_ContentLen  += remain;    m_TotalLen  += remain;    if (m_ContentLen < 64)return;    transform();    while (i  +  64 <= size){memcpy(m_Content, data  +  i, 64);transform();i  += 64;m_TotalLen  += 64;    }    m_ContentLen = size - i;    m_TotalLen  += m_ContentLen;    memcpy(m_Content, data  +  i, m_ContentLen);}unsigned char* cMd5::getCode(){    unsigned char * code = new unsigned char[16];    memcpy(code, &m_A, 4);    memcpy(code  +  4, &m_B, 4);    memcpy(code  +  8, &m_C, 4);    memcpy(code  +  12, &m_D, 4);    return code;}void cMd5::clearMemoryHelper(char* p){    delete []p;}



Text/Xin Xinyuan ChongReprinted please indicate the source http://blog.csdn.net/yxstars/article/details/39031467






What are the following strings? MD5: FB57887C5FFDE54ACBEE17C3F24640D9

A brief description of the MD5 algorithm can be: MD5 processes input information in 512-bit groups, and each group is divided into 16 32-seat groups, after a series of processing, the algorithm output consists of four 32-bit groups. After these four 32-bit groups are cascade, a 128-bit hash value is generated. In the MD5 algorithm, you first need to fill in the information so that the bitwise length is equal to 512 for the result of 448 remainder. Therefore, the bit Length (Bits Length) of the information is extended to N * 512 + 448, that is, N * 64 + 56 Bytes (Bytes), and N is a non-negative integer, N can be zero. The filling method is as follows. Fill in one and countless zeros after the information until the above conditions are met. Then, append a length of the prefill information in 64-bit binary format to the result. After the two steps, the bit length of the current information is N * 512 + 448 + 64 = (N + 1) * 512, that is, the length is exactly an integer multiple of 512. The reason for this is to meet the requirements for the length of information in subsequent processing. MD5 contains four 32-bit integer parameters called Chaining Variable, which are A = 0x67452301, B = 0xefcdab89, C = 0x98badcfe, D = 0x10325476. After these four linked variables are set, the four-round cyclic Operation of the algorithm is started. The number of cycles is the number of 512-bit information groups in the information. Copy the above four linked variables to the other four variables: A to a, B to B, C to c, D to d. The main cycle has four rounds (MD4 only has three rounds), and each cycle is very similar. Perform 16 operations in the first round. Each operation performs a non-linear function operation on three of them in a, B, c, and d. Then, the resulting result is added with the fourth variable, a sub-group of the text, and a constant. Move the result to the left ring and add one of a, B, c, or d. Finally, replace one of a, B, c, or d with this result. Here are the four non-linear functions used in each operation (one in each round ). F (X, Y, Z) = (X & Y) | ((~ X) & Z) G (X, Y, Z) = (X & Z) | (Y &(~ Z) H (X, Y, Z) = X ^ Y ^ z I (X, Y, Z) = Y ^ (X | (~ Z) (& yes And, | yes or ,~ No, ^ is an exclusive or): If the corresponding bits of X, Y, and Z are independent and even, then each bit of the result should be independent and even. F is a bitwise operation function. That is, if X, then Y, otherwise Z. Function H is a bitwise parity operator. Assume that Mj represents the j sub-group of the message (from 0 to 15). The constant ti is an integer of 4294967296 * abs (sin (I). The I value ranges from 1 to 64, unit: radians. (4294967296 is equal to the power of 2.) FF (a, B, c, d, Mj, s, ti) indicates a = B + (a + F (B, c, d) + Mj + ti) <s) GG (a, B, c, d, Mj, s, ti) indicates a = B + (a + G (B, c, d) + Mj + ti) <s) HH (a, B, c, d, Mj, s, ti) represents a = B + (a + H (B, c, d) + Mj + ti) <s) II (a, B, c, d, Mj, s, ti) indicates that a = B + (a + I (B, c, d) + Mj + ti) <s) the four rounds (64 steps) are: FF (a, B, c, d, M0, 7, 0xd76aa478) FF (d, a, B, c, M1, 12, 0xe8c7b756) FF (c, d, a, B, M2, 17, 0x242070db) FF (B, c, d ...... remaining full text>

A usable MD5 algorithm requires a primary function.

Copy the following code and save it as md5lib. h. Then, in the C code:
# Include "md5lib. h"
Where:
Char * MDString (char * string); MD5
Char * MDFile (char * filename); MD5
The following code is used:
//--------------------------------
/* 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 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.
*/

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

# Include <stdio. h>
# Include <time. h>
# Include <string. h>

# Define MD 5

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

/* PROTOTYPES shocould 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.
*/
# Ifndef PROTOTYPES
# Define PROTOTYPES 0
# Endif

/* POINTER defines a generic pointer type */
Typedef unsigned char * POINTER;

/* UINT2 defines a two byte word */
Typedef unsigned short int UINT2;

/* UINT4 defines a four byte word */
Typedef unsigned long int UINT4;

/* PROTO_LIST is defined depending... the remaining full text>

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.