<form method="post">
<input type="text" name="text" size="100" value="<?echo $_POST[text]?>" />
<input type="submit" value="Hash"/>
</form>
<?
function rev0rd($hex){
return substr($hex,6,2).substr($hex,4,2).substr($hex,2,2).substr($hex,0,2);
}
function binhex($binario){
$array = array( "0000"=>"0",
"0001"=>"1",
"0010"=>"2",
"0011"=>"3",
"0100"=>"4",
"0101"=>"5",
"0110"=>"6",
"0111"=>"7",
"1000"=>"8",
"1001"=>"9",
"1010"=>"A",
"1011"=>"B",
"1100"=>"C",
"1101"=>"D",
"1110"=>"E",
"1111"=>"F");
$longitud = strlen($binario);
$chars = $longitud / 8;
$hexdecimal="";
for ($i=1; $i <= $longitud; $i+=8){
$char = substr($binario,$i-1,8);
$aHex = $array[substr($char,0,4)];
$bHex = $array[substr($char,4,4)];
$hexdecimal .= "$aHex$bHex";
}
return $hexdecimal;
}
function my_md5($text){
//Convertir el texto en binario
$nStr = strlen($text);
if ($nStr != 0){
for ($i=1; $i<= $nStr; $i++){
$binario .= str_pad(decbin(ord($text[$i-1])), 8, "0", STR_PAD_LEFT);
}
}
//Calcular bits de relleno
$nBits = strlen($binario);
switch ($nBits){
Case ($nBits < 448):
echo "$nBits < 448 <br>";
$nBits = 448 - $nBits;
break;
Case ($nBits > 448):
echo "$nBits > 448 <br>";
$nBits = 512 - ($nBits - 448);
break;
case ($nBits == 448):
echo "$nBits = 448 <br>";
//$nBits = $nBits + 512; //448 + 512 = 960 % 512 = 64
$nBits = $nBits + 576; //448 + 576 = 1024 % 512 = 0
break;
}
//Agregar 1 y rellenar con 0
$binario .= str_pad("1",$nBits,"0");
// Agregar la longitud del texto en codigo binario representado en 64bits
$binario .= str_pad(decbin($nStr),64,"0", STR_PAD_LEFT);
// Mostrar El Binario
echo "<textarea cols='90' rows='20'>";
$caracteres = strlen($binario);
for ($i=1; $i<=$caracteres;$i++){
echo $binario[$i-1];
if ($i%8 == 0 & $i > 0){
echo " ";
}
if ($i%80 == 0 & $i > 0){
echo "\r\n";
}
}
echo strlen($binario)." Bits</textarea><br>";
// Funcion LeftRotate
Function leftrotate($x, $n){
return ((($x) << ($n)) | (($x) >> (32-($n))));
}
//Preparacion de la tabla "MD5 checksum"
$s = array( 0 => 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22 );
$s += array( 16 => 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20 );
$s += array( 32 => 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23 );
$s += array( 48 => 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21 );
//Preparacin de Constantes
for ($i=0; $i<= 63; $i++){
$K[$i] = floor((abs(sin($i+1)) * pow(2,32)));
// Ver tabla de constantes
//echo ($i+1)." = 0x".dechex($K[$i])."<br>";
}
//Funciones auxiliares
function F($x,$y,$z){
$x = str_pad(decbin($x),32,"0",STR_PAD_LEFT);
$y = str_pad(decbin($y),32,"0",STR_PAD_LEFT);
$z = str_pad(decbin($z),32,"0",STR_PAD_LEFT);
for ($i=0; $i<=31; $i++){
$F .= strlen(($x[$i] and $y[$i]) or (!$x[$i] and $z[$i]));
}
return bindec($F);
}
function G($x,$y,$z){
$x = str_pad(decbin($x),32,"0",STR_PAD_LEFT);
$y = str_pad(decbin($y),32,"0",STR_PAD_LEFT);
$z = str_pad(decbin($z),32,"0",STR_PAD_LEFT);
for ($i=0; $i<=31; $i++){
$G .= strlen(($x[$i] and $z[$i]) or (!$z[$i] and $y[$i]));
}
return bindec($G);
}
function H($x,$y,$z){
$x = str_pad(decbin($x),32,"0",STR_PAD_LEFT);
$y = str_pad(decbin($y),32,"0",STR_PAD_LEFT);
$z = str_pad(decbin($z),32,"0",STR_PAD_LEFT);
for ($i=0; $i<=31; $i++){
$H .= strlen(($x[$i] xor $y[$i] xor $z[$i]));
}
return bindec($H);
}
function I($x,$y,$z){
$x = str_pad(decbin($x),32,"0",STR_PAD_LEFT);
$y = str_pad(decbin($y),32,"0",STR_PAD_LEFT);
$z = str_pad(decbin($z),32,"0",STR_PAD_LEFT);
for ($i=0; $i<=31; $i++){
$I .= strlen(($y[$i] xor ($x[$i] or (!$z[$i]))));
}
return bindec($I);
}
//Inicializacion de variables
$A0 = 0x01234567; //0x67452301
$B0 = 0x89abcdef; //0xefcdab89
$C0 = 0xfedcba98; //0x98badcfe
$D0 = 0x76543210; //0x10325476
//Corte en bloques de 512 bits
for ($chunk = 0; $chunk<= strlen($binario); $chunk+=512){
$bloque = substr($binario, $chunk, 512);
if (strlen($bloque)>0){
// Creacion de 16 palabras de 32 bits en Hexadecimal
$hexdecimal = binhex($bloque);
$longitud = strlen($hexdecimal);
$chars = $longitud / 8;
//Mostrar los bloques y sus respectivas palabras
echo "<div style='display: table-cell; padding: 0px 5px;'>".strlen($bloque)." Bits<br>";
for ($i=1; $i <= $longitud; $i=$i+8){
$w[$i-1] = substr($hexdecimal,$i-1,8);
echo "0x".$w[$i-1]."<br>";
}
echo "</div>";
$AA = $A0;
$BB = $B0;
$CC = $C0;
$DD = $D0;
// Raunds MD5
for ($i=0; $i<=63; $i++){
switch ($i){
case ( 0 <= $i and $i <= 15):
$F = F($B,$C,$D);
$g = $i;
break;
case (16 <= $i and $i <= 31):
$F = G($B,$C,$D);
$g = (5*$i + 1) % 16;
break;
case (32 <= $i and $i <= 47):
$F = H($B,$C,$D);
$g = (3*$i + 5) % 16;
break;
case (48 <= $i and $i <= 63):
$F = I($B,$C,$D);
$g = (7*$i) % 16;
break;
}
$dTemp = $D;
$D = $C;
$C = $B;
$B = $B + leftrotate(($A + $F + $K[$i] + $w[$g]), $s[$i]);
$A = $dTemp;
}
$A0 = $A0 + $A;
$B0 = $B0 + $B;
$C0 = $C0 + $C;
$D0 = $D0 + $D;
}
}
return rev0rd(dechex($A0)).rev0rd(dechex($B0)).rev0rd(dechex($C0)).rev0rd(dechex($D0));
}
$text=$_POST[text];
$checkSum=md5($text);
$test=my_md5($text);
echo "<b>$checkSum</b> Es el Hash de \"<b>$text</b>\"<br>";
echo "<b>$test</b> Es mi resultado";
?>