用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);
$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=$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;
新闻热点
疑难解答