Home Backend Development PHP Tutorial 用PHP写的MD5加密函数_PHP

用PHP写的MD5加密函数_PHP

Jun 01, 2016 pm 12:34 PM
intval function encryption

用PHP写的MD5加密函数

//php_md5("字符串")
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 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 ($iShiftBits31) { }
        $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(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(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(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(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/BITS_TO_A_BYTE))+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        {
                $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));//这行可能有问题
        }
        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        {
                $AA=$a;
                $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,0xEAA127FA);
        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;

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Tips for dynamically creating new functions in golang functions Tips for dynamically creating new functions in golang functions Apr 25, 2024 pm 02:39 PM

Go language provides two dynamic function creation technologies: closure and reflection. closures allow access to variables within the closure scope, and reflection can create new functions using the FuncOf function. These technologies are useful in customizing HTTP routers, implementing highly customizable systems, and building pluggable components.

Considerations for parameter order in C++ function naming Considerations for parameter order in C++ function naming Apr 24, 2024 pm 04:21 PM

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

How to write efficient and maintainable functions in Java? How to write efficient and maintainable functions in Java? Apr 24, 2024 am 11:33 AM

The key to writing efficient and maintainable Java functions is: keep it simple. Use meaningful naming. Handle special situations. Use appropriate visibility.

Complete collection of excel function formulas Complete collection of excel function formulas May 07, 2024 pm 12:04 PM

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

C++ Function Exception Advanced: Customized Error Handling C++ Function Exception Advanced: Customized Error Handling May 01, 2024 pm 06:39 PM

Exception handling in C++ can be enhanced through custom exception classes that provide specific error messages, contextual information, and perform custom actions based on the error type. Define an exception class inherited from std::exception to provide specific error information. Use the throw keyword to throw a custom exception. Use dynamic_cast in a try-catch block to convert the caught exception to a custom exception type. In the actual case, the open_file function throws a FileNotFoundException exception. Catching and handling the exception can provide a more specific error message.

Things to note when Golang functions receive map parameters Things to note when Golang functions receive map parameters Jun 04, 2024 am 10:31 AM

When passing a map to a function in Go, a copy will be created by default, and modifications to the copy will not affect the original map. If you need to modify the original map, you can pass it through a pointer. Empty maps need to be handled with care, because they are technically nil pointers, and passing an empty map to a function that expects a non-empty map will cause an error.

Get the gate.io installation package for free Get the gate.io installation package for free Feb 21, 2025 pm 08:21 PM

Gate.io is a popular cryptocurrency exchange that users can use by downloading its installation package and installing it on their devices. The steps to obtain the installation package are as follows: Visit the official website of Gate.io, click "Download", select the corresponding operating system (Windows, Mac or Linux), and download the installation package to your computer. It is recommended to temporarily disable antivirus software or firewall during installation to ensure smooth installation. After completion, the user needs to create a Gate.io account to start using it.

The magic formula for PHP function execution efficiency The magic formula for PHP function execution efficiency Apr 23, 2024 pm 10:21 PM

To solve the problem of low efficiency of PHP function execution, you can follow the following magic formula: reduce the number of parameters, use parameter passing by reference, shorten the length of the function body, and reduce the depth of function calls. Practical example: Passing parameters by reference can significantly improve performance. Best practices include: avoiding unnecessary calls, using caching, analyzing performance bottlenecks, and following coding standards.

See all articles