«
用PHP写的MD5加密函数

时间:2008-5-31    作者:Deri    分类: 分享


   <code>以下为引用的内容:<br />  //PHP_md5("字符串")<br />define("BITS_TO_A_BYTE",8);<br />define("BYTES_TO_A_WORD",4);<br />define("BITS_TO_A_WORD",32);<br />$m_lOnBits=array(30);<br />$m_l2Power=array(30);<br />  function LShift($lValue,$iShiftBits)<br />{<br />    if ($iShiftBits==0) return $lValue;<br />    if ($iShiftBits==31)<br />    {<br />        if ($lValue&1) { return 0x80000000; }<br />        else { return 0; }<br />    }<br />    if ($iShiftBits < 0 || $iShiftBits > 31) { }<br />    if (($lValue&$GLOBALS[31-$iShiftBits]))<br />    {    $tmpstr=(($lValue&$GLOBALS[31-($iShiftBits+1)])*$GLOBALS[$iShiftBits])|0x80000000; }<br />    else<br />    { $tmpstr=(($lValue&$GLOBALS[31-$iShiftBits])*$GLOBALS[$iShiftBits]); }<br />    return $tmpstr;<br />}<br />  function RShift($lValue,$iShiftBits)<br />{<br />    if ($iShiftBits==0)return $lValue;<br />    if ($iShiftBits==31)<br />    {<br />        if ($lValue&0x80000000) { return 1; }<br />        else { return 0; }<br />    }<br />    if ($iShiftBits<0 || $iShiftBits>31) { }<br />    $tmpstr=floor(($lValue&0x7FFFFFFE)/$GLOBALS[$iShiftBits]);<br />    if ($lValue&0x80000000) { $tmpstr=$tmpstr|floor(0x40000000/$GLOBALS[$iShiftBits-1]); }<br />    return $tmpstr;<br />}<br />  function RotateLeft($lValue,$iShiftBits)<br />{<br />    return LShift($lValue,$iShiftBits)|RShift($lValue,(32-$iShiftBits));<br />} <br />  function AddUnsigned($lX,$lY)<br />{<br />    $lX8=$lX&0x80000000;<br />    $lY8=$lY&0x80000000;<br />    $lX4=$lX&0x40000000;<br />    $lY4=$lY&0x40000000;<br />    $lResult=($lX&0x3FFFFFFF)+($lY&0x3FFFFFFF); <br />  if ($lX4&$lY4) { $lResult=$lResult^0x80000000^$lX8^$lY8; }<br />    if ($lX4|$lY4)<br />    {<br />        if ($lResult&0x40000000)<br />        { $lResult=$lResult^0xC0000000^$lX8^$lY8; }<br />        else<br />        { $lResult=$lResult^0x40000000^$lX8^$lY8; }<br />    }<br />    else<br />    { $lResult=$lResult^$lX8^$lY8; }<br />    return $lResult;<br />} <br />  function md5_F($x,$y,$z)<br />{<br />    return ($x&$y)|((~$x)&$z);<br />}<br />  function md5_G($x,$y,$z)<br />{<br />    return ($x&$z)|($y&(~$z));<br />}<br />  function md5_H($x,$y,$z)<br />{<br />    return ($x^$y^$z);<br />} <br />  function md5_I($x,$y,$z)<br />{<br />    return ($y^($x|(~$z)));<br />} <br />  function md5_FF(&$a,$b,$c,$d,$x,$s,$ac)<br />{<br />    $a=AddUnsigned($a,AddUnsigned(AddUnsigned(md5_F($b,$c,$d),$x),$ac));<br />    $a=RotateLeft($a,$s);<br />    $a=AddUnsigned($a,$b);<br />} <br />  function md5_GG(&$a,$b,$c,$d,$x,$s,$ac)<br />{<br />    $a=AddUnsigned($a,AddUnsigned(AddUnsigned(md5_G($b,$c,$d),$x),$ac));<br />    $a=RotateLeft($a,$s);<br />    $a=AddUnsigned($a,$b);<br />}<br />  function md5_HH(&$a,$b,$c,$d,$x,$s,$ac)<br />{<br />    $a=AddUnsigned($a,AddUnsigned(AddUnsigned(md5_H($b,$c,$d),$x),$ac));<br />    $a=RotateLeft($a,$s);<br />    $a=AddUnsigned($a,$b);<br />}<br />  function md5_II(&$a,$b,$c,$d,$x,$s,$ac)<br />{<br />    $a=AddUnsigned($a,AddUnsigned(AddUnsigned(md5_I($b,$c,$d),$x),$ac));<br />    $a=RotateLeft($a,$s);<br />    $a=AddUnsigned($a,$b);<br />} <br />  function ConvertToWordArray($sMessage)<br />{<br />    $lWordArray=array();<br />    $MODULUS_BITS=512;<br />    $CONGRUENT_BITS=448;<br />    $lMessageLength=strlen($sMessage);<br />    $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);<br />    $lBytePosition=0;<br />    $lByteCount=0;<br />    while(!($lByteCount>=$lMessageLength))<br />    {<br />        $lWordCount=floor($lByteCount/BYTES_TO_A_WORD);<br />        $lBytePosition=($lByteCount%BYTES_TO_A_WORD)*BITS_TO_A_BYTE; Chinaz@com<br />        $lWordArray[$lWordCount]=$lWordArray[$lWordCount]|LShift(ord(substr($sMessage,$lByteCount+1-1,1)),$lBytePosition);<br />        $lByteCount=$lByteCount+1;<br />    }<br />    $lWordCount=floor($lByteCount/BYTES_TO_A_WORD);<br />    $lBytePosition=($lByteCount%BYTES_TO_A_WORD)*BITS_TO_A_BYTE;<br />    $lWordArray[$lWordCount]=$lWordArray[$lWordCount]|LShift(0x80,$lBytePosition);<br />    $lWordArray[$lNumberOfWords-2]=LShift($lMessageLength,3);<br />    $lWordArray[$lNumberOfWords-1]=RShift($lMessageLength,29);<br />    return $lWordArray;<br />}<br />  function WordToHex($lValue)<br />{<br />    $tmpstr="";<br />    for ($lCount=0; $lCount<=3; $lCount++)<br />    {<br />        $lByte=RShift($lValue,$lCount*BITS_TO_A_BYTE)&$GLOBALS[BITS_TO_A_BYTE-1];<br />        $tmpstr=$tmpstr.(substr("0".dechex($lByte),strlen("0".dechex($lByte))-2));//这行可能有问题<br />    }<br />    return $tmpstr;<br />}<br />function PHP_MD5($sMessage)<br />{<br />    $GLOBALS[0]=intval(1);<br />    $GLOBALS[1]=intval(3);<br />    $GLOBALS[2]=intval(7);<br />    $GLOBALS[3]=intval(15);<br />    $GLOBALS[4]=intval(31);<br />    $GLOBALS[5]=intval(63);<br />    $GLOBALS[6]=intval(127);<br />    $GLOBALS[7]=intval(255);<br />    $GLOBALS[8]=intval(511);<br />    $GLOBALS[9]=intval(1023);<br />    $GLOBALS[10]=intval(2047);<br />    $GLOBALS[11]=intval(4095);<br />    $GLOBALS[12]=intval(8191);<br />    $GLOBALS[13]=intval(16383);<br />    $GLOBALS[14]=intval(32767);<br />    $GLOBALS[15]=intval(65535);<br />    $GLOBALS[16]=intval(131071);<br />    $GLOBALS[17]=intval(262143); <br />    $GLOBALS[18]=intval(524287);<br />    $GLOBALS[19]=intval(1048575);<br />    $GLOBALS[20]=intval(2097151);<br />    $GLOBALS[21]=intval(4194303);<br />    $GLOBALS[22]=intval(8388607);<br />    $GLOBALS[23]=intval(16777215);<br />    $GLOBALS[24]=intval(33554431);<br />    $GLOBALS[25]=intval(67108863);<br />    $GLOBALS[26]=intval(134217727);<br />    $GLOBALS[27]=intval(268435455);<br />    $GLOBALS[28]=intval(536870911);<br />    $GLOBALS[29]=intval(1073741823);<br />    $GLOBALS[30]=intval(2147483647); <br />  $GLOBALS[0]=intval(1);<br />    $GLOBALS[1]=intval(2);<br />    $GLOBALS[2]=intval(4);<br />    $GLOBALS[3]=intval(8);<br />    $GLOBALS[4]=intval(16);<br />    $GLOBALS[5]=intval(32);<br />    $GLOBALS[6]=intval(64);<br />    $GLOBALS[7]=intval(128);<br />    $GLOBALS[8]=intval(256);<br />    $GLOBALS[9]=intval(512);<br />    $GLOBALS[10]=intval(1024);<br />    $GLOBALS[11]=intval(2048);<br />    $GLOBALS[12]=intval(4096);<br />    $GLOBALS[13]=intval(8192); <br />    $GLOBALS[14]=intval(16384);<br />    $GLOBALS[15]=intval(32768);<br />    $GLOBALS[16]=intval(65536);<br />    $GLOBALS[17]=intval(131072);<br />    $GLOBALS[18]=intval(262144);<br />    $GLOBALS[19]=intval(524288);<br />    $GLOBALS[20]=intval(1048576);<br />    $GLOBALS[21]=intval(2097152);<br />    $GLOBALS[22]=intval(4194304);<br />    $GLOBALS[23]=intval(8388608);<br />    $GLOBALS[24]=intval(16777216);<br />    $GLOBALS[25]=intval(33554432);<br />    $GLOBALS[26]=intval(67108864);<br />    $GLOBALS[27]=intval(134217728);<br />    $GLOBALS[28]=intval(268435456);<br />    $GLOBALS[29]=intval(536870912);<br />    $GLOBALS[30]=intval(1073741824); <br />  $S11=7;<br />    $S12=12;<br />    $S13=17;<br />    $S14=22;<br />    $S21=5;<br />    $S22=9;<br />    $S23=14;<br />    $S24=20;<br />    $S31=4;<br />    $S32=11;<br />    $S33=16;<br />    $S34=23;<br />    $S41=6;<br />    $S42=10;<br />    $S43=15;<br />    $S44=21;<br />  $x=ConvertToWordArray($sMessage); <br />  $a=0x67452301;<br />    $b=0xEFCDAB89;<br />    $c=0x98BADCFE;<br />    $d=0x10325476;<br />  for ($k=0; $k<=count($x); $k=$k+16)<br />    {<br />        $AA=$a;<br />        $BB=$b;<br />        $CC=$c;<br />        $DD=$d;<br />        md5_FF($a,$b,$c,$d,$x[$k+0],$S11,0xD76AA478);<br />    md5_FF($d,$a,$b,$c,$x[$k+1],$S12,0xE8C7B756);<br />    md5_FF($c,$d,$a,$b,$x[$k+2],$S13,0x242070DB);<br />    md5_FF($b,$c,$d,$a,$x[$k+3],$S14,0xC1BDCEEE);<br />    md5_FF($a,$b,$c,$d,$x[$k+4],$S11,0xF57C0FAF); <br />    md5_FF($d,$a,$b,$c,$x[$k+5],$S12,0x4787C62A);<br />    md5_FF($c,$d,$a,$b,$x[$k+6],$S13,0xA8304613);<br />    md5_FF($b,$c,$d,$a,$x[$k+7],$S14,0xFD469501);<br />    md5_FF($a,$b,$c,$d,$x[$k+8],$S11,0x698098D8);<br />    md5_FF($d,$a,$b,$c,$x[$k+9],$S12,0x8B44F7AF);<br />    md5_FF($c,$d,$a,$b,$x[$k+10],$S13,0xFFFF5BB1);<br />    md5_FF($b,$c,$d,$a,$x[$k+11],$S14,0x895CD7BE);<br />    md5_FF($a,$b,$c,$d,$x[$k+12],$S11,0x6B901122);<br />    md5_FF($d,$a,$b,$c,$x[$k+13],$S12,0xFD987193);<br />    md5_FF($c,$d,$a,$b,$x[$k+14],$S13,0xA679438E);<br />    md5_FF($b,$c,$d,$a,$x[$k+15],$S14,0x49B40821);<br />  md5_GG($a,$b,$c,$d,$x[$k+1],$S21,0xF61E2562);<br />    md5_GG($d,$a,$b,$c,$x[$k+6],$S22,0xC040B340);<br />    md5_GG($c,$d,$a,$b,$x[$k+11],$S23,0x265E5A51);<br />    md5_GG($b,$c,$d,$a,$x[$k+0],$S24,0xE9B6C7AA);<br />    md5_GG($a,$b,$c,$d,$x[$k+5],$S21,0xD62F105D);<br />    md5_GG($d,$a,$b,$c,$x[$k+10],$S22,0x2441453);<br />    md5_GG($c,$d,$a,$b,$x[$k+15],$S23,0xD8A1E681);<br />    md5_GG($b,$c,$d,$a,$x[$k+4],$S24,0xE7D3FBC8);<br />    md5_GG($a,$b,$c,$d,$x[$k+9],$S21,0x21E1CDE6);<br />    md5_GG($d,$a,$b,$c,$x[$k+14],$S22,0xC33707D6);<br />    md5_GG($c,$d,$a,$b,$x[$k+3],$S23,0xF4D50D87); <br />    md5_GG($b,$c,$d,$a,$x[$k+8],$S24,0x455A14ED);<br />    md5_GG($a,$b,$c,$d,$x[$k+13],$S21,0xA9E3E905);<br />    md5_GG($d,$a,$b,$c,$x[$k+2],$S22,0xFCEFA3F8);<br />    md5_GG($c,$d,$a,$b,$x[$k+7],$S23,0x676F02D9);<br />    md5_GG($b,$c,$d,$a,$x[$k+12],$S24,0x8D2A4C8A);<br />  md5_HH($a,$b,$c,$d,$x[$k+5],$S31,0xFFFA3942);<br />    md5_HH($d,$a,$b,$c,$x[$k+8],$S32,0x8771F681);<br />    md5_HH($c,$d,$a,$b,$x[$k+11],$S33,0x6D9D6122);<br />    md5_HH($b,$c,$d,$a,$x[$k+14],$S34,0xFDE5380C);<br />    md5_HH($a,$b,$c,$d,$x[$k+1],$S31,0xA4BEEA44);<br />    md5_HH($d,$a,$b,$c,$x[$k+4],$S32,0x4BDECFA9);<br />    md5_HH($c,$d,$a,$b,$x[$k+7],$S33,0xF6BB4B60);<br />    md5_HH($b,$c,$d,$a,$x[$k+10],$S34,0xBEBFBC70);<br />    md5_HH($a,$b,$c,$d,$x[$k+13],$S31,0x289B7EC6);<br />    md5_HH($d,$a,$b,$c,$x[$k+0],$S32,0xEAA127FA);<br />    md5_HH($c,$d,$a,$b,$x[$k+3],$S33,0xD4EF3085);<br />    md5_HH($b,$c,$d,$a,$x[$k+6],$S34,0x4881D05);<br />    md5_HH($a,$b,$c,$d,$x[$k+9],$S31,0xD9D4D039);<br />    md5_HH($d,$a,$b,$c,$x[$k+12],$S32,0xE6DB99E5);<br />    md5_HH($c,$d,$a,$b,$x[$k+15],$S33,0x1FA27CF8);<br />    md5_HH($b,$c,$d,$a,$x[$k+2],$S34,0xC4AC5665);<br />  md5_II($a,$b,$c,$d,$x[$k+0],$S41,0xF4292244);<br />    md5_II($d,$a,$b,$c,$x[$k+7],$S42,0x432AFF97);<br />    md5_II($c,$d,$a,$b,$x[$k+14],$S43,0xAB9423A7);<br />    md5_II($b,$c,$d,$a,$x[$k+5],$S44,0xFC93A039);<br />    md5_II($a,$b,$c,$d,$x[$k+12],$S41,0x655B59C3);<br />    md5_II($d,$a,$b,$c,$x[$k+3],$S42,0x8F0CCC92);<br />    md5_II($c,$d,$a,$b,$x[$k+10],$S43,0xFFEFF47D);<br />    md5_II($b,$c,$d,$a,$x[$k+1],$S44,0x85845DD1);<br />    md5_II($a,$b,$c,$d,$x[$k+8],$S41,0x6FA87E4F);<br />    md5_II($d,$a,$b,$c,$x[$k+15],$S42,0xFE2CE6E0);<br />    md5_II($c,$d,$a,$b,$x[$k+6],$S43,0xA3014314); <br />    md5_II($b,$c,$d,$a,$x[$k+13],$S44,0x4E0811A1);<br />    md5_II($a,$b,$c,$d,$x[$k+4],$S41,0xF7537E82);<br />    md5_II($d,$a,$b,$c,$x[$k+11],$S42,0xBD3AF235);<br />    md5_II($c,$d,$a,$b,$x[$k+2],$S43,0x2AD7D2BB);<br />    md5_II($b,$c,$d,$a,$x[$k+9],$S44,0xEB86D391);<br />        $a=AddUnsigned($a,$AA);<br />        $b=AddUnsigned($b,$BB);<br />        $c=AddUnsigned($c,$CC);<br />        $d=AddUnsigned($d,$DD);<br />    }<br />    return strtolower(WordToHex($a).WordToHex($b).WordToHex($c).WordToHex($d)); <br />} <br />  $aaa=PHP_MD5("sdfasdf");<br />echo $aaa;</code></p>