MD5 encryption function written in PHP _ PHP

Source: Internet
Author: User
PHP-written MD5 encryption function php_md5 (string) define (BITS_TO_A_BYTE, 8); define (BYTES_TO_A_WORD, 4); define (BITS_TO_A_WORD, 32); $ m_lOnBitsarray (30 ); $ m_l2Powerarray (30); functionLShift ($ lValue, $ iShiftBits) {if ($ iShiftBits0) return $ lValue; if MD5 encryption function written in PHP

// Php_md5 ("string ")
Define ("BITS_TO_A_BYTE", 8 );
Define ("BYTES_TO_A_WORD", 4 );
Define ("BITS_TO_A_WORD", 32 );
$ M_lOnBits = array (30 );
$ M_l2Power = array (30 );

Function LShift ($ lValue, $ iShiftBits)
{
If ($ iShiftBits = 0) return $ lValue;
If ($ iShiftBits = 31)
{
If ($ lValue & 1) {return 0x80000000 ;}
Else {return 0 ;}
}
If ($ iShiftBits <0 | $ iShiftBits> 31 ){}
If ($ lValue & $ GLOBALS [31-$ iShiftBits])
{$ Tmpstr = ($ lValue & $ GLOBALS [31-($ iShiftBits + 1)]) * $ GLOBALS [$ iShiftBits]) | 0x80000000 ;}
Else
{$ Tmpstr = ($ lValue & $ GLOBALS [31-$ iShiftBits]) * $ GLOBALS [$ iShiftBits]);}
Return $ tmpstr;
}

Function RShift ($ lValue, $ iShiftBits)
{
If ($ iShiftBits = 0) return $ lValue;
If ($ iShiftBits = 31)
{
If ($ lValue & 0x80000000) {return 1 ;}
Else {return 0 ;}
}
If ($ iShiftBits <0 | $ iShiftBits> 31 ){}
$ Tmpstr = floor ($ lValue & 0x7FFFFFFE)/$ GLOBALS [$ iShiftBits]);
If ($ lValue & 0x80000000) {$ tmpstr = $ tmpstr | floor (0x40000000/$ GLOBALS [$ iShiftBits-1]);}
Return $ tmpstr;
}

Function RotateLeft ($ lValue, $ iShiftBits)
{
Return LShift ($ lValue, $ iShiftBits) | RShift ($ lValue, (32-$ iShiftBits ));
}

Function AddUnsigned ($ lX, $ lY)
{
$ LX8 = $ lX & 0x80000000;
$ LY8 = $ lY & 0x80000000;
$ LX4 = $ lX & 0x40000000;
$ LY4 = $ lY & 0x40000000;
$ LResult = ($ lX & 0x3FFFFFFF) + ($ lY & 0x3FFFFFFF );

If ($ lX4 & $ lY4) {$ lResult = $ lResult ^ 0x80000000 ^ $ lX8 ^ $ lY8 ;}
If ($ lX4 | $ lY4)
{
If ($ lResult & 0x40000000)
{$ LResult = $ lResult ^ 0xC0000000 ^ $ lX8 ^ $ lY8 ;}
Else
{$ LResult = $ lResult ^ 0x40000000 ^ $ lX8 ^ $ lY8 ;}
}
Else
{$ LResult = $ lResult ^ $ lX8 ^ $ lY8 ;}
Return $ lResult;
}

Function md5_F ($ x, $ y, $ z)
{
Return ($ x & $ y) | ((~ $ X) & $ z );
}

Function md5_G ($ x, $ y, $ z)
{
Return ($ x & $ z) | ($ y &(~ $ Z ));
}

Function md5_H ($ x, $ y, $ z)
{
Return ($ x ^ $ y ^ $ z );
}

Function md5_ I ($ x, $ y, $ z)
{
Return ($ y ^ ($ x | (~ $ Z )));
}

Function md5_FF (& $ a, $ B, $ c, $ d, $ x, $ s, $ ac)
{
$ A = AddUnsigned ($ a, AddUnsigned (md5_F ($ B, $ c, $ d), $ x), $ ac ));
$ A = RotateLeft ($ a, $ s );
$ A = AddUnsigned ($ a, $ B );
}

Function md5_GG (& $ a, $ B, $ c, $ d, $ x, $ s, $ ac)
{
$ A = AddUnsigned ($ a, AddUnsigned (md5_G ($ B, $ c, $ d), $ x), $ ac ));
$ A = RotateLeft ($ a, $ s );
$ A = AddUnsigned ($ a, $ B );
}

Function md5_HH (& $ a, $ B, $ c, $ d, $ x, $ s, $ ac)
{
$ A = AddUnsigned ($ a, AddUnsigned (md5_H ($ B, $ c, $ d), $ x), $ ac ));
$ A = RotateLeft ($ a, $ s );
$ A = AddUnsigned ($ a, $ B );
}

Function md5_II (& $ a, $ B, $ c, $ d, $ x, $ s, $ ac)
{
$ A = AddUnsigned ($ a, AddUnsigned (md5_ I ($ B, $ c, $ d), $ x), $ ac ));
$ A = RotateLeft ($ a, $ s );
$ A = AddUnsigned ($ a, $ B );
}

Function ConvertToWordArray ($ sMessage)
{
$ LWordArray = array ();
$ MODULUS_BITS = 512;
$ CONGRUENT_BITS = 448;
$ LMessageLength = strlen ($ sMessage );
$ LNumberOfWords = (floor ($ lMessageLength + floor ($ MODULUS_BITS-$ CONGRUENT_BITS)/BITS_TO_A_BYTE)/floor ($ MODULUS_BITS/second) + 1) * floor ($ MODULUS_BITS/BITS_TO_A_WORD );
$ LBytePosition = 0;
$ LByteCount = 0;
While (! ($ LByteCount >=$ lMessageLength ))
{
$ LWordCount = floor ($ lByteCount/BYTES_TO_A_WORD );
$ LBytePosition = ($ lByteCount % BYTES_TO_A_WORD) * BITS_TO_A_BYTE;
$ LWordArray [$ lWordCount] = $ lWordArray [$ lWordCount] | LShift (ord (substr ($ sMessage, $ lByteCount + 1-1, 1), $ lBytePosition );
$ LByteCount = $ lByteCount + 1;
}
$ LWordCount = floor ($ lByteCount/BYTES_TO_A_WORD );
$ LBytePosition = ($ lByteCount % BYTES_TO_A_WORD) * BITS_TO_A_BYTE;
$ LWordArray [$ lWordCount] = $ lWordArray [$ lWordCount] | LShift (0x80, $ lBytePosition );
$ LWordArray [$ lNumberOfWords-2] = LShift ($ lMessageLength, 3 );
$ LWordArray [$ lNumberOfWords-1] = RShift ($ lMessageLength, 29 );
Return $ lWordArray;
}

Function WordToHex ($ lValue)
{
$ Tmpstr = "";
For ($ lCount = 0; $ lCount <= 3; $ lCount ++)
{
$ LByte = RShift ($ lValue, $ lCount * BITS_TO_A_BYTE) & $ GLOBALS [BITS_TO_A_BYTE-1];
$ Tmpstr = $ tmpstr. (substr ("0 ". dechex ($ lByte), strlen ("0 ". dechex ($ lByte)-2); // this line may be faulty.
}
Return $ tmpstr;
}
Function php_MD5 ($ sMessage)
{
$ GLOBALS [0] = intval (1 );
$ GLOBALS [1] = intval (3 );
$ GLOBALS [2] = intval (7 );
$ GLOBALS [3] = intval (15 );
$ GLOBALS [4] = intval (31 );
$ GLOBALS [5] = intval (63 );
$ GLOBALS [6] = intval (127 );
$ GLOBALS [7] = intval (255 );
$ GLOBALS [8] = intval (511 );
$ GLOBALS [9] = intval (1023 );
$ GLOBALS [10] = intval (2047 );
$ GLOBALS [11] = intval (4095 );
$ GLOBALS [12] = intval (8191 );
$ GLOBALS [13] = intval (16383 );
$ GLOBALS [14] = intval (32767 );
$ GLOBALS [15] = intval (65535 );
$ GLOBALS [16] = intval (131071 );
$ GLOBALS [17] = intval (262143 );
$ GLOBALS [18] = intval (524287 );
$ GLOBALS [19] = intval (1048575 );
$ GLOBALS [20] = intval (2097151 );
$ GLOBALS [21] = intval (4194303 );
$ GLOBALS [22] = intval (8388607 );
$ GLOBALS [23] = intval (16777215 );
$ GLOBALS [24] = intval (33554431 );
$ GLOBALS [25] = intval (67108863 );
$ GLOBALS [26] = intval (134217727 );
$ GLOBALS [27] = intval (268435455 );
$ GLOBALS [28] = intval (536870911 );
$ GLOBALS [29] = intval (1073741823 );
$ GLOBALS [30] = intval (2147483647 );

$ GLOBALS [0] = intval (1 );
$ GLOBALS [1] = intval (2 );
$ GLOBALS [2] = intval (4 );
$ GLOBALS [3] = intval (8 );
$ GLOBALS [4] = intval (16 );
$ GLOBALS [5] = intval (32 );
$ GLOBALS [6] = intval (64 );
$ GLOBALS [7] = intval (128 );
$ GLOBALS [8] = intval (256 );
$ GLOBALS [9] = intval (512 );
$ GLOBALS [10] = intval (1024 );
$ GLOBALS [11] = intval (2048 );
$ GLOBALS [12] = intval (4096 );
$ GLOBALS [13] = intval (8192 );
$ GLOBALS [14] = intval (16384 );
$ GLOBALS [15] = intval (32768 );
$ GLOBALS [16] = intval (65536 );
$ GLOBALS [17] = intval (131072 );
$ GLOBALS [18] = intval (262144 );
$ GLOBALS [19] = intval (524288 );
$ GLOBALS [20] = intval (1048576 );
$ GLOBALS [21] = intval (2097152 );
$ GLOBALS [22] = intval (4194304 );
$ GLOBALS [23] = intval (8388608 );
$ GLOBALS [24] = intval (16777216 );
$ GLOBALS [25] = intval (33554432 );
$ GLOBALS [26] = intval (67108864 );
$ GLOBALS [27] = intval (134217728 );
$ GLOBALS [28] = intval (268435456 );
$ GLOBALS [29] = intval (536870912 );
$ GLOBALS [30] = intval (1073741824 );

$ S11 = 7;
$ S12 = 12;
$ S13 = 17;
$ S14 = 22;
$ S21 = 5;
$ S22 = 9;
$ S23 = 14;
$ S24 = 20;
$ S31 = 4;
$ S32 = 11;
$ S33 = 16;
$ S34 = 23;
$ S41 = 6;
$ S42 = 10;
$ S43 = 15;
$ S44 = 21;

$ X = ConvertToWordArray ($ sMessage );

$ A = 0x67452301;
$ B = 0xEFCDAB89;
$ C = 0x98BADCFE;
$ D = 0x10325476;

For ($ k = 0; $ k <= count ($ x); $ k = $ k + 16)
{
$ AA = $;
$ BB = $ B;
$ CC = $ c;
$ DD = $ d;
Md5_FF ($ a, $ B, $ c, $ d, $ x [$ k + 0], $ S11, 0xD76AA478 );
Md5_FF ($ d, $ a, $ B, $ c, $ x [$ k + 1], $ S12, 0xE8C7B756 );
Md5_FF ($ c, $ d, $ a, $ B, $ x [$ k + 2], $ S13, 0x242070DB );
Md5_FF ($ B, $ c, $ d, $ a, $ x [$ k + 3], $ S14, 0xC1BDCEEE );
Md5_FF ($ a, $ B, $ c, $ d, $ x [$ k + 4], $ S11, 0xF57C0FAF );
Md5_FF ($ d, $ a, $ B, $ c, $ x [$ k + 5], $ S12, 0x4787C62A );
Md5_FF ($ c, $ d, $ a, $ B, $ x [$ k + 6], $ S13, 0xA8304613 );
Md5_FF ($ B, $ c, $ d, $ a, $ x [$ k + 7], $ S14, 0xFD469501 );
Md5_FF ($ a, $ B, $ c, $ d, $ x [$ k + 8], $ S11, 0x698098D8 );
Md5_FF ($ d, $ a, $ B, $ c, $ x [$ k + 9], $ S12, 0x8B44F7AF );
Md5_FF ($ c, $ d, $ a, $ B, $ x [$ k + 10], $ S13, 0xFFFF5BB1 );
Md5_FF ($ B, $ c, $ d, $ a, $ x [$ k + 11], $ S14, 0x895CD7BE );
Md5_FF ($ a, $ B, $ c, $ d, $ x [$ k + 12], $ S11, 0x6B901122 );
Md5_FF ($ d, $ a, $ B, $ c, $ x [$ k + 13], $ S12, 0xFD987193 );
Md5_FF ($ c, $ d, $ a, $ B, $ x [$ k + 14], $ S13, 0xA679438E );
Md5_FF ($ B, $ c, $ d, $ a, $ x [$ k + 15], $ S14, 0x49B40821 );

Md5_GG ($ a, $ B, $ c, $ d, $ x [$ k + 1], $ S21, 0xF61E2562 );
Md5_GG ($ d, $ a, $ B, $ c, $ x [$ k + 6], $ S22, 0xC040B340 );
Md5_GG ($ c, $ d, $ a, $ B, $ x [$ k + 11], $ S23, 0x265E5A51 );
Md5_GG ($ B, $ c, $ d, $ a, $ x [$ k + 0], $ S24, 0xE9B6C7AA );
Md5_GG ($ a, $ B, $ c, $ d, $ x [$ k + 5], $ S21, 0xD62F105D );
Md5_GG ($ d, $ a, $ B, $ c, $ x [$ k + 10], $ S22, 0x2441453 );
Md5_GG ($ c, $ d, $ a, $ B, $ x [$ k + 15], $ S23, 0xD8A1E681 );
Md5_GG ($ B, $ c, $ d, $ a, $ x [$ k + 4], $ S24, 0xE7D3FBC8 );
Md5_GG ($ a, $ B, $ c, $ d, $ x [$ k + 9], $ S21, 0x21E1CDE6 );
Md5_GG ($ d, $ a, $ B, $ c, $ x [$ k + 14], $ S22, 0xC33707D6 );
Md5_GG ($ c, $ d, $ a, $ B, $ x [$ k + 3], $ S23, 0xF4D50D87 );
Md5_GG ($ B, $ c, $ d, $ a, $ x [$ k + 8], $ S24, 0x455A14ED );
Md5_GG ($ a, $ B, $ c, $ d, $ x [$ k + 13], $ S21, 0xA9E3E905 );
Md5_GG ($ d, $ a, $ B, $ c, $ x [$ k + 2], $ S22, 0xFCEFA3F8 );
Md5_GG ($ c, $ d, $ a, $ B, $ x [$ k + 7], $ S23, 0x676F02D9 );
Md5_GG ($ B, $ c, $ d, $ a, $ x [$ k + 12], $ S24, 0x8D2A4C8A );

Md5_HH ($ a, $ B, $ c, $ d, $ x [$ k + 5], $ S31, 0xFFFA3942 );
Md5_HH ($ d, $ a, $ B, $ c, $ x [$ k + 8], $ S32, 0x8771F681 );
Md5_HH ($ c, $ d, $ a, $ B, $ x [$ k + 11], $ S33, 0x6D9D6122 );
Md5_HH ($ B, $ c, $ d, $ a, $ x [$ k + 14], $ S34, 0xFDE5380C );
Md5_HH ($ a, $ B, $ c, $ d, $ x [$ k + 1], $ S31, 0xA4BEEA44 );
Md5_HH ($ d, $ a, $ B, $ c, $ x [$ k + 4], $ S32, 0x4BDECFA9 );
Md5_HH ($ c, $ d, $ a, $ B, $ x [$ k + 7], $ S33, 0xF6BB4B60 );
Md5_HH ($ B, $ c, $ d, $ a, $ x [$ k + 10], $ S34, 0xBEBFBC70 );
Md5_HH ($ a, $ B, $ c, $ d, $ x [$ k + 13], $ S31, 0x289B7EC6 );
Md5_HH ($ d, $ a, $ B, $ c, $ x [$ k + 0], $ S32, 0xeaa1_fa );
Md5_HH ($ c, $ d, $ a, $ B, $ x [$ k + 3], $ S33, 0xD4EF3085 );
Md5_HH ($ B, $ c, $ d, $ a, $ x [$ k + 6], $ S34, 0x4881D05 );
Md5_HH ($ a, $ B, $ c, $ d, $ x [$ k + 9], $ S31, 0xD9D4D039 );
Md5_HH ($ d, $ a, $ B, $ c, $ x [$ k + 12], $ S32, 0xE6DB99E5 );
Md5_HH ($ c, $ d, $ a, $ B, $ x [$ k + 15], $ S33, 0x1FA27CF8 );
Md5_HH ($ B, $ c, $ d, $ a, $ x [$ k + 2], $ S34, 0xC4AC5665 );

Md5_II ($ a, $ B, $ c, $ d, $ x [$ k + 0], $ S41, 0xF4292244 );
Md5_II ($ d, $ a, $ B, $ c, $ x [$ k + 7], $ S42, 0x432AFF97 );
Md5_II ($ c, $ d, $ a, $ B, $ x [$ k + 14], $ S43, 0xAB9423A7 );
Md5_II ($ B, $ c, $ d, $ a, $ x [$ k + 5], $ S44, 0xFC93A039 );
Md5_II ($ a, $ B, $ c, $ d, $ x [$ k + 12], $ S41, 0x655B59C3 );
Md5_II ($ d, $ a, $ B, $ c, $ x [$ k + 3], $ S42, 0x8F0CCC92 );
Md5_II ($ c, $ d, $ a, $ B, $ x [$ k + 10], $ S43, 0xFFEFF47D );
Md5_II ($ B, $ c, $ d, $ a, $ x [$ k + 1], $ S44, 0x85845DD1 );
Md5_II ($ a, $ B, $ c, $ d, $ x [$ k + 8], $ S41, 0x6FA87E4F );
Md5_II ($ d, $ a, $ B, $ c, $ x [$ k + 15], $ S42, 0xFE2CE6E0 );
Md5_II ($ c, $ d, $ a, $ B, $ x [$ k + 6], $ S43, 0xA3014314 );
Md5_II ($ B, $ c, $ d, $ a, $ x [$ k + 13], $ S44, 0x4E0811A1 );
Md5_II ($ a, $ B, $ c, $ d, $ x [$ k + 4], $ S41, 0xF7537E82 );
Md5_II ($ d, $ a, $ B, $ c, $ x [$ k + 11], $ S42, 0xBD3AF235 );
Md5_II ($ c, $ d, $ a, $ B, $ x [$ k + 2], $ S43, 0x2AD7D2BB );
Md5_II ($ B, $ c, $ d, $ a, $ x [$ k + 9], $ S44, 0xEB86D391 );
$ A = AddUnsigned ($ a, $ AA );
$ B = AddUnsigned ($ B, $ BB );
$ C = AddUnsigned ($ c, $ CC );
$ D = AddUnsigned ($ d, $ DD );
}
Return strtolower (WordToHex ($ a). WordToHex ($ B). WordToHex ($ c). WordToHex ($ d ));
}

$ Aaa = php_MD5 ("sdfasdf ");
Echo $ aaa;

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.