Referring to the definition of Wiki, hash function (or hash algorithm) is a method to create a small number "fingerprint" from any data. This function breaks down the data and creates a new fingerprint called the hash value. Hash values are generally used to represent a string consisting of short random letters and numbers. Good hash functions rarely conflict with hash columns in the input domain. In the case of hash and data processing, conflicts are not allowed to distinguish data, which makes it more difficult to locate database records. (For specific terms, please write your own terms)
MD5 is a one-way hash algorithm. It is short for message-Digest algorithm 5 (Information-Digest algorithm), developed by md2, md3, and md4. Here, the so-called one-way, only the final value can be calculated backward, and the initial value cannot be calculated through reverse calculation.
MD5:
Input any length of information. After processing, the output is 128-bit information (digital fingerprint)
Different results (uniqueness) obtained from different inputs)
According to the output result of 128 bits, the input information cannot be reversed (irreversible)
MD5 algorithm steps:
1. Makeup
First, we need to perform a makeup so that the length of the information after the completion is 512 and the remainder is 448. That is, data is extended
K * 512 + 448 (BIT), that is, K * 64 + 56 (byte), where K is a natural number. Specific bit filling operation: first fill in 1, followed by 0 to meet the above requirements. At least 1 bit is required, and a maximum of BIT is required.
2. Fill Length
Add 8 bytes based on K * 64 + 56 (byte), which is used to save the length of the original information.
3. initialize the MD buffer.
Use a four 32-bit buffer (A, B, C, D) to calculate the MD5 value. The initialization uses the hexadecimal format. Note that the low byte is before:
Word A: 01 23 45 67
Word B: 89 AB CD ef
Word C: Fe DC Ba 98
Word D: 76 54 32 10
Because the memory storage direction and time writing are the opposite, the definition should be:
# Define a 0x67452301ul
# Define B 0xefcdab89ul
# Define c 0x98badcfeul
# Define D 0x10325476ul
4. Auxiliary Functions and encryption functions
Define four auxiliary 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 ))))
Then define four encryption functions:
# Define rotate_left (x, n) (x) <(N) | (x)> (32-(N ))))
# Define ff (a, B, c, d, X, S, AC ){\
(A) + = f (B), (c), (d) + (x) + AC ;\
(A) = rotate_left (a), (s ));\
(A) + = (B );\
}
# Define Gg (a, B, c, d, X, S, AC ){\
(A) + = g (B), (c), (d) + (x) + AC ;\
(A) = rotate_left (a), (s ));\
(A) + = (B );\
}
# Define HH (a, B, c, d, X, S, AC ){\
(A) + = H (B), (c), (d) + (x) + AC ;\
(A) = rotate_left (a), (s ));\
(A) + = (B );\
}
# Define II (a, B, c, d, X, S, AC ){\
(A) + = I (B), (c), (d) + (x) + AC ;\
(A) = rotate_left (a), (s ));\
(A) + = (B );\
}
Download specific c ++ Implementation Algorithms
Encryption hash algorithm-MD5