Use PHP to implement the MD5 algorithm

Source: Internet
Author: User
MD5 is the Message-DigestAlgorithm5 (information-Digest Algorithm 5 ). The MD5 algorithm is a message digest algorithm that uses any length of information as input for computation and generates a 128-bit (16-byte) fingerprint or message digest (fingerprintormessagedigest ). The probability that two different messages generate the same information abstract is quite small.

In the previous article <Content-MD5 field> we have mentioned the MD5 algorithm, here the MD5 algorithm is used to verify the integrity of the file, which is consistent with the application scenario of the MD5 algorithm, that is, it was widely used in a variety of programming languages in 1990s to ensure that data is transmitted without error.

MD5 is Message-Digest Algorithm 5 (information-Digest Algorithm 5 ). The MD5 algorithm is a message digest algorithm that computes messages of any length and generates a 128-bit (16-byte) message) (fingerprint or message digest ). The probability that two different information digests generate the same information is quite small. it is less likely that the original information is generated reversely from a given information Digest. However, due to the collision, MD5 is not suitable for scenarios with high security requirements as the computer's computing capability increases. However, it is still possible to deal with general file integrity checks.

The standard implementation process of the MD5 algorithm can be divided into five steps.

1. the MD5 algorithm supplements the input data so that if the data bit length LEN is used to calculate the remainder of 512, the result is 448.

That is, data is extended to K512 + 448 bits. That is, K64 + 56 bytes, and K is an integer. Specific fill operation: Fill in 1, and then fill 0 to meet the above requirements

2. Data population length

Use a 64-bit number to represent the original length of data B, and use two 32-digit digits to represent B. In this case, the data is filled into a multiple of 512 bits.

3. initialize the MD5 parameter

Four 32-bit integers (A, B, C, D) are used to calculate information summaries. the initialization uses hexadecimal numbers.

  • A = 0X01234567
  • B = 0X89abcdef
  • C = 0Xfedcba98
  • D = 0X76543210

4. bitwise operation functions

X, Y, and Z are 32-bit integers.

  • F (X, Y, Z) = X & Y | NOT (X) & Z
  • G (X, Y, Z) = X & Z | Y? (Z)
  • H (X, Y, Z) = X xor Y xor Z
  • I (X, Y, Z) = Y xor (X | not (Z ))

5. main transformation process

The common array T [1... 64] is used. the 32-bit integer T is represented in hexadecimal notation, and the data is represented in a 16-bit integer array M.

The specific process is as follows:

/* Process the original data */For I = 0 to N/16-1 do/* each time, store the original data in array X of 16 elements. */For j = 0 to 15 do Set X [j] to M [I * 16 + j]. end/end the cycle of J/* Save A as AA, B as BB, C as CC, and D as DD. */AA = a bb = B cc = c dd = D/* 1st Round */* use [abcd k s I] to indicate the following operations: * a = B + (a + F (B, c, d) + X [k] + T) s ). * // * Do the following 16 operations. */[ABCD 0 7 1] [DABC 1 12 2] [CDAB 2 17 3] [BCDA 3 22 4] [ABCD 4 7 5] [DABC 5 12 6] [CDAB 6 17 7] [BCDA 7 22 8] [ABCD 8 7 9] [DABC 9 12 10] [CDAB 10 17 11] [BCDA 11 22 12] [ABCD 12 7 13] [DABC 13 12 14] [CDAB 14 17 15] [BCDA 15 22 16]/* 2nd wheel ** // ** use [abcd k s I] to represent the following operation * = B + (a + G (B, c, d) + X [k] + T) s ). * // * Do the following 16 operations. */[ABCD 1 5 17] [DABC 6 9 18] [CDAB 11 14 19] [BCDA 0 20 20] [ABCD 5 5 5 21] [DABC 10 9 22] [CDAB 15 14 23] [BCDA 4 20 24] [ABCD 9 5 25] [DABC 14 9 26] [CDAB 3 14 27] [BCDA 8 20 28] [ABCD 13 5 29] [DABC 2 9 30] [CDAB 7 14 31] [BCDA 12 20 32]/* 3rd Round * // ** use [abcd k s I] to represent the following operation * a = B + (a + H (B, c, d) + X [k] + T) s ). * // * Do the following 16 operations. */[ABCD 5 4 33] [DABC 8 11 34] [CDAB 11 16 35] [BCDA 14 23 36] [ABCD 1 4 37] [DABC 4 11 38] [CDAB 7 16 39] [BCDA 10 23 40] [ABCD 13 4 41] [DABC 0 11 42] [CDAB 3 16 43] [BCDA 6 23 44] [ABCD 9 4 45] [DABC 12 11 46] [CDAB 15 16 47] [BCDA 2 23 48]/* 4th wheel * // ** use [abcd k s I] to represent the following operations * a = B + (a + I (B, c, d) + X [k] + T) s ). * // * Do the following 16 operations. */[ABCD 0 6 49] [DABC 7 10 50] [CDAB 14 15 51] [BCDA 5 21 52] [ABCD 12 6 53] [DABC 3 10 54] [CDAB 10 15 55] [BCDA 1 21 56] [ABCD 8 6 57] [DABC 15 10 58] [CDAB 6 15 59] [BCDA 13 21 60] [ABCD 4 6 61] [DABC 11 10 62] [CDAB 2 15 63] [BCDA 9 21 64]/* perform the following operations */A = A + aa B = B + BB C = C + cc d = D + DD end/* end the I loop *
Use PHP to implement the MD5 algorithm

PHP is basically implemented according to the above algorithm,
For PHP, there are three special points:

  • It is necessary to avoid the automatic conversion of array elements when they exceed the integer length;
  • Implementation of the unsigned right shift operation;
  • Converts a string to an 8-bit data structure stored as an element.

The code is as follows:

 GetDigist (); echo"
", Md5 ($ str); class MD5 {const CHAR_ALIGNMENT = 8; private $ _ digist; private $ _ state; public function _ construct ($ str) {$ bin = $ this-> _ str2bin ($ str); $ len = strlen ($ str) * self: CHAR_ALIGNMENT; $ bin [$ len> 5] | = 128 <($ len % 32); $ bin [($ len + 64)> 9) <4) + 14] = $ len; $ this-> _ md5Init (); $ this-> _ update ($ bin ); $ this-> _ digist = $ this-> _ bin2hex ($ this-> _ state);}/*** public method * get information abstract * @ retur N string */public function getDigist () {return $ this-> _ digist;} private function _ bin2hex ($ bin) {$ hex_tab = "0123456789 abcdef "; $ str = ""; for ($ I = 0; $ I <count ($ bin) * 4; $ I ++) {$ str. = $ hex_tab [($ bin [$ I> 2]> ($ I % 4) * 8 + 4) & 0xF]. $ hex_tab [($ bin [$ I> 2] >>( ($ I % 4) * 8) & 0xF] ;}return $ str ;} private function _ update ($ bin) {$ bin_len = count ($ bin); for ($ I = 0; $ I <$ bin_l En; $ I + = 16) {$ block = array (); for ($ j = 0; $ j <16; $ j ++) {$ block [$ j] + = isset ($ bin [$ I + $ j])? $ Bin [$ I + $ j]: 0;} $ this-> _ md5Transform ($ block); unset ($ block );}} /*** initialize */private function _ md5Init () {$ this-> _ state [0] = intval (0x67452301 ); $ this-> _ state [1] = intval (0xefcdab89); $ this-> _ state [2] = intval (0x98badcfe ); $ this-> _ state [3] = intval (0x10325476); return TRUE;} private function _ md5Transform ($ block) {$ a = $ this-> _ state [0]; $ B = $ this-> _ state [1]; $ c = $ this-> _ state [2]; $ d = $ This-> _ state [3]; $ x = $ block;/** Round 1 */MD5Tool: FF ($ a, $ B, $ c, $ d, $ x [0], MD5Tool: S11, 0xd76aa478);/* 1 */MD5Tool: FF ($ d, $ a, $ B, $ c, $ x [1], MD5Tool: S12, 0xe8c7b756);/* 2 */MD5Tool: FF ($ c, $ d, $ a, $ B, $ x [2], MD5Tool: S13, 0x242070db);/* 3 */MD5Tool: FF ($ B, $ c, $ d, $, $ x [3], MD5Tool: S14, 0xc1bdceee);/* 4 */MD5Tool: FF ($ a, $ B, $ c, $ d, $ x [4], MD5Tool: S11, 0xf57c0faf);/* 5 */MD5To Ol: FF ($ d, $ a, $ B, $ c, $ x [5], MD5Tool: S12, 0x4787c62a);/* 6 */MD5Tool :: FF ($ c, $ d, $ a, $ B, $ x [6], MD5Tool: S13, 0xa8304613);/* 7 */MD5Tool :: FF ($ B, $ c, $ d, $ a, $ x [7], MD5Tool: S14, 0xfd469501);/* 8 */MD5Tool :: FF ($ a, $ B, $ c, $ d, $ x [8], MD5Tool: S11, 0x698098d8);/* 9 */MD5Tool :: FF ($ d, $ a, $ B, $ c, $ x [9], MD5Tool: S12, 0x8b44f7af);/* 10 */MD5Tool :: FF ($ c, $ d, $ a, $ B, $ x [10], MD5Tool: S13, 0xff Ff5bb1);/* 11 */MD5Tool: FF ($ B, $ c, $ d, $ a, $ x [11], MD5Tool: S14, 0x895cd7be ); /* 12 */MD5Tool: FF ($ a, $ B, $ c, $ d, $ x [12], MD5Tool: S11, 0x6b901122 ); /* 13 */MD5Tool: FF ($ d, $ a, $ B, $ c, $ x [13], MD5Tool: S12, 0xfd987193 ); /* 14 */MD5Tool: FF ($ c, $ d, $ a, $ B, $ x [14], MD5Tool: S13, 0xa679438e ); /* 15 */MD5Tool: FF ($ B, $ c, $ d, $ a, $ x [15], MD5Tool: S14, 0x49b40821 ); /* 16 * // ** Round 2 */MD5Tool: GG ($ a, $ B, $ c, $ d, $ x [1], MD5Tool: S21, 0xf61e2562);/* 17 */MD5Tool :: GG ($ d, $ a, $ B, $ c, $ x [6], MD5Tool: S22, 0xc040b340);/* 18 */MD5Tool :: GG ($ c, $ d, $ a, $ B, $ x [11], MD5Tool: S23, 0x265e5a51);/* 19 */MD5Tool :: GG ($ B, $ c, $ d, $ a, $ x [0], MD5Tool: S24, 0xe9b6c7aa);/* 20 */MD5Tool :: GG ($ a, $ B, $ c, $ d, $ x [5], MD5Tool: S21, 0xd62f105d);/* 21 */MD5Tool :: GG ($ d, $ a, $ B, $ c, $ x [10], MD5Tool: S22, 0 X2441453);/* 22 */MD5Tool: GG ($ c, $ d, $ a, $ B, $ x [15], MD5Tool: S23, 0xd8a1e681 ); /* 23 */MD5Tool: GG ($ B, $ c, $ d, $ a, $ x [4], MD5Tool: S24, 0xe7d3fbc8 ); /* 24 */MD5Tool: GG ($ a, $ B, $ c, $ d, $ x [9], MD5Tool: S21, 0x21e1cde6 ); /* 25 */MD5Tool: GG ($ d, $ a, $ B, $ c, $ x [14], MD5Tool: S22, 0xc33707d6 ); /* 26 */MD5Tool: GG ($ c, $ d, $ a, $ B, $ x [3], MD5Tool: S23, 0xf4d50d87 ); /* 27 */MD5Tool: GG ($ B, $ c, $ d, $ A, $ x [8], MD5Tool: S24, 0x455a14ed);/* 28 */MD5Tool: GG ($ a, $ B, $ c, $ d, $ x [13], MD5Tool: S21, 0xa9e3e905);/* 29 */MD5Tool: GG ($ d, $ a, $ B, $ c, $ x [2], MD5Tool: S22, 0xfcefa3f8);/* 30 */MD5Tool: GG ($ c, $ d, $ a, $ B, $ x [7], MD5Tool: S23, 0x676f02d9);/* 31 */MD5Tool: GG ($ B, $ c, $ d, $, $ x [12], MD5Tool: S24, 0x8d2a4c8a);/* 32 * // ** Round 3 */MD5Tool: HH ($ a, $ B, $ c, $ d, $ x [5], MD5Tool: S31, 0x Fffa3942);/* 33 */MD5Tool: HH ($ d, $ a, $ B, $ c, $ x [8], MD5Tool: S32, 0x8771f681 ); /* 34 */MD5Tool: HH ($ c, $ d, $ a, $ B, $ x [11], MD5Tool: S33, 0x6d9d6122 ); /* 35 */MD5Tool: HH ($ B, $ c, $ d, $ a, $ x [14], MD5Tool: S34, 0xfde5380c ); /* 36 */MD5Tool: HH ($ a, $ B, $ c, $ d, $ x [1], MD5Tool: S31, 0xa4beea44 ); /* 37 */MD5Tool: HH ($ d, $ a, $ B, $ c, $ x [4], MD5Tool: S32, 0x4bdecfa9 ); /* 38 */MD5Tool: HH ($ c, $ d, $, $ B, $ x [7], MD5Tool: S33, 0xf6bb4b60);/* 39 */MD5Tool: HH ($ B, $ c, $ d, $, $ x [10], MD5Tool: S34, 0xbebfbc70);/* 40 */MD5Tool: HH ($ a, $ B, $ c, $ d, $ x [13], MD5Tool: S31, 0x289b7ec6);/* 41 */MD5Tool: HH ($ d, $ a, $ B, $ c, $ x [0], MD5Tool: S32, 0xeaa127fa);/* 42 */MD5Tool: HH ($ c, $ d, $ a, $ B, $ x [3], MD5Tool: S33, 0xd4ef3085);/* 43 */MD5Tool: HH ($ B, $ c, $ d, $, $ x [6], MD5Tool: S34, 0x4881d05);/* 44 */MD5Tool: HH ($ a, $ B, $ c, $ d, $ x [9], MD5Tool: S31, 0xd9d4d039);/* 45 */MD5Tool:: HH ($ d, $ a, $ B, $ c, $ x [12], MD5Tool: S32, 0xe6db99e5);/* 46 */MD5Tool :: HH ($ c, $ d, $ a, $ B, $ x [15], MD5Tool: S33, 0x1fa27cf8);/* 47 */MD5Tool :: HH ($ B, $ c, $ d, $ a, $ x [2], MD5Tool: S34, 0xc4ac5665 ); /* 48 * // ** Round 4 */MD5Tool: II ($ a, $ B, $ c, $ d, $ x [0], MD5Tool: S41, 0xf4292244);/* 49 */MD5Tool: II ($ d, $ a, $ B, $ C, $ x [7], MD5Tool: S42, 0x432aff97);/* 50 */MD5Tool: II ($ c, $ d, $ a, $ B, $ x [14], MD5Tool: S43, 0xab9423a7);/* 51 */MD5Tool: II ($ B, $ c, $ d, $, $ x [5], MD5Tool: S44, 0xfc93a039);/* 52 */MD5Tool: II ($ a, $ B, $ c, $ d, $ x [12], MD5Tool: S41, 0x655b59c3);/* 53 */MD5Tool: II ($ d, $ a, $ B, $ c, $ x [3], MD5Tool: S42, 0x8f0ccc92);/* 54 */MD5Tool: II ($ c, $ d, $ a, $ B, $ x [10], MD5Tool: S43, 0xffeff47d);/* 5 5 */MD5Tool: II ($ B, $ c, $ d, $ a, $ x [1], MD5Tool: S44, 0x85845dd1 ); /* 56 */MD5Tool: II ($ a, $ B, $ c, $ d, $ x [8], MD5Tool: S41, 0x6fa87e4f ); /* 57 */MD5Tool: II ($ d, $ a, $ B, $ c, $ x [15], MD5Tool: S42, 0xfe2ce6e0 ); /* 58 */MD5Tool: II ($ c, $ d, $ a, $ B, $ x [6], MD5Tool: S43, 0xa3014314 ); /* 59 */MD5Tool: II ($ B, $ c, $ d, $ a, $ x [13], MD5Tool: S44, 0x4e0811a1 ); /* 60 */MD5Tool: II ($ a, $ B, $ c, $ d, $ x [4], MD5 Tool: S41, 0xf7537e82);/* 61 */MD5Tool: II ($ d, $ a, $ B, $ c, $ x [11], MD5Tool :: s42, 0xbd3af235);/* 62 */MD5Tool: II ($ c, $ d, $ a, $ B, $ x [2], MD5Tool: S43, 0x2ad7d2bb);/* 63 */MD5Tool: II ($ B, $ c, $ d, $ a, $ x [9], MD5Tool: S44, 0xeb86d391 ); /* 64 * // *** NOTE, the intval function */$ this-> _ state [0] = intval ($ this-> _ state [0] + $ a) must be executed here ); $ this-> _ state [1] = intval ($ this-> _ state [1] + $ B); $ this-> _ state [2] = intval ($ This-> _ state [2] + $ c ); $ this-> _ state [3] = intval ($ this-> _ state [3] + $ d);} private function _ str2bin ($ str) {$ bin = array (); $ alignment = (1 <self: CHAR_ALIGNMENT)-1; $ len = strlen ($ str); for ($ I = 0; $ I <$ len * self: CHAR_ALIGNMENT; $ I + = self: CHAR_ALIGNMENT) {$ key = $ I> 5; $ bin [$ key] | = (ord ($ str [$ I/self: CHAR_ALIGNMENT]) & $ alignment) <($ I % 32 );} return $ bin ;}} class MD5Tool {/** The S11-S44 is originally a 4*4 matrix, which is implemented with # define in C implementation, * represented as a constant of the class, share among various objects */const S11 = 7; const S12 = 12; const S13 = 17; const S14 = 22; const S21 = 5; const S22 = 9; const S23 = 14; const S24 = 20; const S31 = 4; const S32 = 11; const S33 = 16; const S34 = 23; const S41 = 6; const S42 = 10; const S43 = 15; const S44 = 21;/** F, G, H, I are four basic MD5 functions. * In C implementation, macro implementation is generally used, here we provide */public static in the form of class methods Function F ($ x, $ y, $ z) {return ($ x & $ y) | ((~ $ X) & $ z);} public static function G ($ x, $ y, $ z) {return ($ x & $ z) | ($ y &(~ $ Z);} public static function H ($ x, $ y, $ z) {return $ x ^ $ y ^ $ z;} public static function I ($ x, $ y, $ z) {return $ y ^ ($ x | (~ $ Z);}/*** shift N places left * @ param type $ x * @ param type $ n * @ return type */public static function ROTATE_LEFT ($ x, $ n) {return ($ x <$ n) | self: URShift ($ x, (32-$ n ));} /*** PHP unsigned right shift * @ param type $ x * @ param type $ bits * @ return type */public static function URShift ($ x, $ bits) {/** convert to a string representing a binary number */$ bin = decbin ($ x); $ len = strlen ($ bin ); /** if the length of a string exceeds the upper limit, the minimum length is 32 characters. if the length is insufficient, the upper limit is 0 to 32 characters. */if ($ len> 32) {$ Bin = substr ($ bin, $ len-32, 32);} elseif ($ len <32) {$ bin = str_pad ($ bin, 32, '0 ', STR_PAD_LEFT);}/** retrieve the number of digits to be moved, and fill 0 */return bindec (str_pad (substr ($ bin, 0, 32-$ bits), 32, '0', STR_PAD_LEFT);}/*** FF, GG, HH, and II will call F, G, H, and I for next step transformation * where FF, GG, HH and II are four-wheel transfer calls, respectively. ** Note: In PHP, reference return is used here. The first element * and all return values must be forcibly converted to an integer using intval, otherwise, PHP may automatically convert */public static function FF (& $ a, $ B, $ c, $ d, $ x, $ s, $ ac) {$ A + = self: F ($ B, $ c, $ d) + ($ x) + $ ac; $ a = self: ROTATE_LEFT ($, $ s); $ a = intval ($ a + $ B);} public static function GG (& $ a, $ B, $ c, $ d, $ x, $ s, $ ac) {$ a + = self: G ($ B, $ c, $ d) + ($ x) + $ ac; $ a = self :: ROTATE_LEFT ($ a, $ s); $ a = intval ($ a + $ B);} public static function HH (& $ a, $ B, $ c, $ d, $ x, $ s, $ ac) {$ a + = self: H ($ B, $ c, $ d) + ($ x) + $ ac; $ a = self: ROTATE_LEFT ($ a, $ s); $ a = intval ($ a + $ B);} public static function II (& $ a, $ B, $ c, $ d, $ x, $ s, $ ac) {$ a + = self :: I ($ B, $ c, $ d) + ($ x) + $ ac; $ a = self: ROTATE_LEFT ($ a, $ s ); $ a = intval ($ a + $ B) ;}}?>

In the constructor, the steps of the MD5 algorithm are basically one-to-one.

Postscript

In fact, this article does not have much practical value, unless you want to use PHP to implement the MD5 algorithm and understand the specific MD5 algorithm, unless you are the same as me, a little cheap, unless ......

References
  • Http://zh.wikipedia.org/zh-cn/MD5
  • Http://www.cppblog.com/ant/archive/2007/09/11/31886.html
  • Http://tech.fromeasy.com/bbs/viewthread.php? Tid = 693
  • Http://pajhome.org.uk/crypt/md5/

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.