用PHP写的MD5加密函数
用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 < 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(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<=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));//这行可能有问题
}
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);<
补充:综合编程 , 安全编程 ,