MD5 encryption functions written in PHP

Source: Internet
Author: User
Tags define array functions md5 md5 encryption modulus return strlen
Functions | encryption

MD5 encryption functions 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)
{
$lX 8= $lX &0x80000000;
$lY 8= $lY &0x80000000;
$lX 4= $lX &0x40000000;
$lY 4= $lY &0x40000000;
$lResult = ($lX &0x3fffffff) + ($lY &0x3fffffff);

if ($lX 4& $lY 4) {$lResult = $lResult ^0x80000000^ $lX 8^ $lY 8;}
if ($lX 4| $lY 4)
{
if ($lResult &0x40000000)
{$lResult = $lResult ^0xc0000000^ $lX 8^ $lY 8;}
Else
{$lResult = $lResult ^0x40000000^ $lX 8^ $lY 8;}
}
Else
{$lResult = $lResult ^ $lX 8^ $lY 8;}
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));
$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));
$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));
$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));
$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);//There may be a problem with this line.
}
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);

$S 11=7;
$S 12=12;
$S 13=17;
$S 14=22;
$S 21=5;
$S 22=9;
$S 23=14;
$S 24=20;
$S 31=4;
$S 32=11;
$S 33=16;
$S 34=23;
$S 41=6;
$S 42=10;
$S 43=15;
$S 44=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], $S 11,0xd76aa478);
MD5_FF ($d, $a, $b, $c, $x [$k +1], $S 12,0xe8c7b756);
Md5_ff ($c, $d, $a, $b, $x [$k +2], $S 13,0x242070db);
Md5_ff ($b, $c, $d, $a, $x [$k +3], $S 14,0xc1bdceee);
MD5_FF ($a, $b, $c, $d, $x [$k +4], $S 11,0XF57C0FAF);
MD5_FF ($d, $a, $b, $c, $x [$k +5], $S 12,0x4787c62a);
Md5_ff ($c, $d, $a, $b, $x [$k +6], $S 13,0xa8304613);
Md5_ff ($b, $c, $d, $a, $x [$k +7], $S 14,0xfd469501);
MD5_FF ($a, $b, $c, $d, $x [$k +8], $S 11,0x698098d8);
MD5_FF ($d, $a, $b, $c, $x [$k +9], $S 12,0X8B44F7AF);
Md5_ff ($c, $d, $a, $b, $x [$k +10], $S 13,0XFFFF5BB1);
Md5_ff ($b, $c, $d, $a, $x [$k +11], $S 14,0x895cd7be);
MD5_FF ($a, $b, $c, $d, $x [$k +12], $S 11,0x6b901122);
MD5_FF ($d, $a, $b, $c, $x [$k +13], $S 12,0xfd987193);
Md5_ff ($c, $d, $a, $b, $x [$k +14], $S 13,0xa679438e);
Md5_ff ($b, $c, $d, $a, $x [$k +15], $S 14,0x49b40821);

Md5_gg ($a, $b, $c, $d, $x [$k +1], $S 21,0xf61e2562);
Md5_gg ($d, $a, $b, $c, $x [$k +6], $S 22,0xc040b340);
Md5_gg ($c, $d, $a, $b, $x [$k +11], $S 23,0x265e5a51);
Md5_gg ($b, $c, $d, $a, $x [$k +0], $S 24,0XE9B6C7AA);
Md5_gg ($a, $b, $c, $d, $x [$k +5], $S 21,0xd62f105d);
Md5_gg ($d, $a, $b, $c, $x [$k +10], $S 22,0x2441453);
Md5_gg ($c, $d, $a, $b, $x [$k +15], $S 23,0xd8a1e681);
Md5_gg ($b, $c, $d, $a, $x [$k +4], $S 24,0xe7d3fbc8);
Md5_gg ($a, $b, $c, $d, $x [$k +9], $S 21,0x21e1cde6);
Md5_gg ($d, $a, $b, $c, $x [$k +14], $S 22,0xc33707d6);
Md5_gg ($c, $d, $a, $b, $x [$k +3], $S 23,0xf4d50d87);
Md5_gg ($b, $c, $d, $a, $x [$k +8], $S 24,0x455a14ed);
Md5_gg ($a, $b, $c, $d, $x [$k +13], $S 21,0xa9e3e905);
Md5_gg ($d, $a, $b, $c, $x [$k +2], $S 22,0xfcefa3f8);
Md5_gg ($c, $d, $a, $b, $x [$k +7], $S 23,0x676f02d9);
Md5_gg ($b, $c, $d, $a, $x [$k +12], $S 24,0x8d2a4c8a);

MD5_HH ($a, $b, $c, $d, $x [$k +5], $S 31,0xfffa3942);
MD5_HH ($d, $a, $b, $c, $x [$k +8], $S 32,0x8771f681);
MD5_HH ($c, $d, $a, $b, $x [$k +11], $S 33,0x6d9d6122);
MD5_HH ($b, $c, $d, $a, $x [$k +14], $S 34,0xfde5380c);
MD5_HH ($a, $b, $c, $d, $x [$k +1], $S 31,0xa4beea44);
MD5_HH ($d, $a, $b, $c, $x [$k +4], $S 32,0x4bdecfa9);
MD5_HH ($c, $d, $a, $b, $x [$k +7], $S 33,0xf6bb4b60);
MD5_HH ($b, $c, $d, $a, $x [$k +10], $S 34,0xbebfbc70);
MD5_HH ($a, $b, $c, $d, $x [$k +13], $S 31,0x289b7ec6);
MD5_HH ($d, $a, $b, $c, $x [$k +0], $S 32,0XEAA127FA);
MD5_HH ($c, $d, $a, $b, $x [$k +3], $S 33,0xd4ef3085);
MD5_HH ($b, $c, $d, $a, $x [$k +6], $S 34,0x4881d05);
MD5_HH ($a, $b, $c, $d, $x [$k +9], $S 31,0xd9d4d039);
MD5_HH ($d, $a, $b, $c, $x [$k +12], $S 32,0xe6db99e5);
MD5_HH ($c, $d, $a, $b, $x [$k +15], $S 33,0x1fa27cf8);
MD5_HH ($b, $c, $d, $a, $x [$k +2], $S 34,0xc4ac5665);

Md5_ii ($a, $b, $c, $d, $x [$k +0], $S 41,0xf4292244);
Md5_ii ($d, $a, $b, $c, $x [$k +7], $S 42,0x432aff97);
Md5_ii ($c, $d, $a, $b, $x [$k +14], $S 43,0xab9423a7);
Md5_ii ($b, $c, $d, $a, $x [$k +5], $S 44,0xfc93a039);
Md5_ii ($a, $b, $c, $d, $x [$k +12], $S 41,0x655b59c3);
Md5_ii ($d, $a, $b, $c, $x [$k +3], $S 42,0x8f0ccc92);
Md5_ii ($c, $d, $a, $b, $x [$k +10], $S 43,0xffeff47d);
Md5_ii ($b, $c, $d, $a, $x [$k +1], $S 44,0x85845dd1);
Md5_ii ($a, $b, $c, $d, $x [$k +8], $S 41,0x6fa87e4f);
Md5_ii ($d, $a, $b, $c, $x [$k +15], $S 42,0xfe2ce6e0);
Md5_ii ($c, $d, $a, $b, $x [$k +6], $S 43,0xa3014314);
Md5_ii ($b, $c, $d, $a, $x [$k +13], $S 44,0x4e0811a1);
Md5_ii ($a, $b, $c, $d, $x [$k +4], $S 41,0xf7537e82);
Md5_ii ($d, $a, $b, $c, $x [$k +11], $S 42,0xbd3af235);
Md5_ii ($c, $d, $a, $b, $x [$k +2], $S 43,0X2AD7D2BB);
Md5_ii ($b, $c, $d, $a, $x [$k +9], $S 44,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.