MD5 (Message-digestalgorithm 5) is one of the most widely used hashing algorithms in computers. It can be used to convert data into a fixed set of values , the predecessor of which is mainly MD2, MD3 and MD4 algorithms. The MD2 algorithm was developed by Rivest in 1989 and was later discovered by Rogier and Chauvaud if the checksum was ignored and the MD2 generated a conflict. In order to enhance the security of the algorithm, Rivest in 1990 to develop the MD4 algorithm, followed by Denboer and Bosselaers and others quickly discovered the attack MD4 version of the first step and the third step of the vulnerability, so MD4 was eliminated. 1991 Rivest developed a more mature MD5 algorithm based on the original MD4 algorithm.
The MD5 algorithm is a widely used digest algorithm called the Information Digest, and its main process includes three steps for filling the string (Padding), the fragment summary (digesting), and the output of the final digest (outputting) . The padding refers to the first need to fill the information, so that its byte length to 512 to obtain the result of 448. Padding is filled with a 1 and countless 0 after the information is stopped until the above length condition is met. After the fill is complete, append a 64-bit binary to the length of the pre-fill information, so that the current information byte length is equal to n*512+448+64= (n+1) *512, so that the length can be divisible by 512 to meet the information length can be divided into n+1 a complete grouping of requirements.
Two important evaluation indexes of abstract algorithm are abstract strength and speed. Abstract intensity refers to the ability of the algorithm to resist various attacks, and the speed of encryption is the speed at which the digest is generated. Generally, abstract strength and digest processing speed are mutually exclusive parameters, that is, the greater the strength inevitably increases the time the digest produces, reducing the speed of the digest. In the case of abstract intensity, the speed of the algorithm can be maximized by using various optimization techniques in the implementation.
MD5 algorithm is widely used, including data encryption, data verification, digital signature and so on. In the Linux system, the main application of MD5 to the plaintext encryption, in the file upload and download in order to verify whether the file has been changed, you need to provide the MD5 hash value of the file, and in the digital signature first use MD5 Compute message digest.
The MD5 algorithm is an iterative hash function structure that inputs an arbitrary length of information and outputs a 128-bit message digest. The core of the algorithm is the fragment summary process, which is the process of a, B, C, D, as shown in Figure 1, from A, B, C, D to the result of a digest operation.
Its algorithm implementation process:
(1), Complement: MD5 algorithm in the first to fill the information, so that its number of bits to meet the 512 after the remainder equals 448. That is, the length of the filled message is equal to a multiple of 512 minus 64, even if the original message is worth the length to satisfy the condition also needs to fill, The length padding is done by adding a 1 and an infinite number of 0 at the end of the message until the condition is met.
(2), complement the clear text length: After the first step, the reserved 64 bits of information to indicate the length of the message before being filled, and appended to the information, so that its total length into 512 of the integer is, so convenient to face the group of information. If the original length of a message exceeds 2^64, it is modeled with 2^64.
After you have completed the above two steps, you can group the messages, each set of 512-bit lengths, and each set of values can be represented as 16 32-bit words.
(3), initialization: The algorithm uses a 128-bit buffer to hold the intermediate results and the final hash value, the buffer using 4 32-bit registers (a,b,c,d) to act as the MD5 species four 32-bit link variable (Chaining Variable) initialization, respectively: state[0]=0x67452301, state[1]=0xefcdab89, state[2]=0x98badcfe,state[3]=0x10325476
(4), input: Copy the values of the 4 link variables into the other four variables. A=state[0], b=state[1], c=state[2], d=state[3], by the four new variables and each split 512-bit sub-message group as the input value of the algorithm, into the algorithm's four-wheel loop, and the number of cycles is determined by the amount of messages grouped.
(5), arithmetic: The main loop of the algorithm consists of 4 rounds, each round to perform 16 operations. Each operation is a non-linear function of 3 of the A,b,c,d four variables, followed by a 4th variable, and a sub-group and a constant (constants from the constant table, which has a total of 64 elements, the first constant is 2^32*abs (sin (i))), Then move the entire result left loop one indefinite number, and finally add one of a,b,c or D, this result supersedes the variable a,b,c or D, see figure 1 and Figure 2.
Figure 1 MD5 processing process
Figure 2 MD5 Fragment summary function processing flow
Where M[i], representing the sub-group of the first group I, I is 0--15; T[J] represents the constant of the step.
(6), output: When the N-packets of a message are processed, the last compression function outputs a 128-bit hash value, which is the message digest.
Hash function can be used to convert any length of message compression into fixed-length hash value, commonly known as message digest or digital fingerprint, can be directly applied to data integrity detection.
MD2, MD4, andMD5 is cryptographic hash functions with a, bit output.
1. MD5 (): Compute the MD5 messagedigest of the b<n> bytes at b<d> and place it in b<md> (whichmust has space for md5_digest_length = = bytes of output). If b<md> IsNULL, the digest is placed in a static array.
The following functions is used if the message isnot completely stored in memory:
2. Md5_init (): initializes ab<md5_ctx> structure.
3. Md5_update (): Can be calledrepeatedly with chunks of the message to be hashed (b<len> bytes atb<data>) .
4. Md5_final ():p laces the message digest in B<md>, which must has space for md5_digest_length== bytes of Out Put, and erases the b<md5_ctx>.
Here is the test code:
Cryptotest.h:
#ifndef _cryptotest_h_#define _cryptotest_h_#include <string>using namespace std;typedef enum {general = 0,ECB, Cbc,cfb,ofb,triple_ecb,triple_cbc}crypto_mode;string des_encrypt (const string cleartext, const string key, Crypto_ mode mode); string des_decrypt (const string ciphertext, const string key, Crypto_mode mode); string Rc4_encrypt (const Strin G cleartext, const string key), String Rc4_decrypt (const string ciphertext, const string key), String md5_digest (const Strin g cleartext); #endif//_cryptotest_h_
Md5test.cpp:
#include "stdafx.h" #include <iostream> #include <string> #include <vector> #include <cstdio># Include <iomanip> #include <stdlib.h> #include <openssl/md5.h> #include "cryptotest.h" using Namespace std;string md5_digest (const string cleartext) {string strdigest;unsigned char tmp[16] = {0}; #if 0md5 ((const UNSI gned char*) Cleartext.c_str (), Cleartext.length (), TMP); #elseMD5_CTX C; Md5_init (&C); Md5_update (&c, Cleartext.c_str (), cleartext.length ()); Md5_final (TMP, &C), #endifchar * tmp1 = new char[32 + 1];memset (tmp1, 0, + 1); for (int i = 0; i <; i++) sprintf ( & (Tmp1[i*2]), "%02x", Tmp[i]);//COUT<<HEX<<SETW (2) <<setfill (' 0 ') << (int) tmp[i]; Strdigest = (char*) tmp1;delete [] Tmp1;return strdigest;}
Main.cpp:
#include "stdafx.h" #include "cryptotest.h" #include "TestMemory.h" #include <iostream> #include <string> Using namespace Std;void test_md5 () {string Strsrc[7] = {"", "a", "ABC", "Message digest", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "a BCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 "," 12345678901234567890123456789012345678901234567890123456789012345678901234567890 "};string strDigest[7] = {" d41d8cd98f00b204e9800998ecf8427e "," 0cc175b9c0f1b6a831c399e269772661 "," 900150983cd24fb0d6963f7d28e17f72 "," F96b697d7cb7938d525a2f31aaf161d0 "," c3fcd3d76192e4007dfb496cca67e13b "," d174ab98d277d9f5a5611c2c9f419d9f "," 57edf4a22be3c955ac49da2e2107b67a "};for (int i = 0; i < 7; i++) {String str = md5_digest (strsrc[i]); cout<<str<& Lt;endl;if (strcmp (Strdigest[i].c_str (), STR.C_STR ())! = 0) cout<< "i =" <<i<< "MD5 error!" <<endl;}} int main (int argc, char* argv[]) {test_md5 ();cout<< "OK!!!" <<endl;return 0;}
MD5 theory is excerpted from:
1. Research on snapshot difference algorithm based on simplified MD5 summarization Technology
2. Design and development of security teacher blog system based on MD5 improved algorithm
Introduction to the MD5 of abstract algorithms and examples of MD5 common functions in OpenSSL