PHP - md5 sin la funcion de php

 
Vista:
Imágen de perfil de Alejandro
Val: 1.634
Plata
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

md5 sin la funcion de php

Publicado por Alejandro (839 intervenciones) el 07/05/2015 00:42:59
  • Alejandro se encuentra ahora conectado en el
  • chat de PHP
Estoy tratando de conseguir el hash md5 sin la funcionde php md5().
Estuve leyendo sobre el algoritmo pero me atoro en la parte del las 4 funciones y los raunds, no se que hacer con mi bloque binario.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Function MyMD5($str){
    /* Convertimos Caracter a Binario de 8 bits */
    Function dec2bin($n){
    	for ($i=1; $i<8; $i++){
    		$bin = ($n%2).$bin;
    		$n= $n/2;
    	}
        return str_pad($bin, 8, "0", STR_PAD_LEFT);
    }
 
    /* Agregamos 1 y rellenamos con 0 */
    function relleno($binario){
    	$x  = strlen($binario);
    	switch ($x){
    		Case ($x < 448):
    			$x = 448 - $x;
    	    break;
    		Case ($x > 448):
    			$x =  512 - ($x - 448);
            break;
    		case ($x = 448):
    			$x = $x + 512;
            break;
    	}
    	$binario .= "1";
    	for ($i=1; $i!=$x; $i++){
    		$binario .= "0";
    	}
    return $binario;
    }
 
    /* Obtenemos en binario a 64 bits la longitud de la cadena */
    function lenMsg($cString){
    	$a = dec2bin(strlen($cString) * 8);
    	$w = strlen($a);
    	while ($w < 64){
    		$a = "0".$a;
    		$w++;
    	}
    return $a;
    }
        $caracteres = strlen($str);
        for ($i=0; $i<$caracteres; $i++){
            $binario .= dec2bin(ord($str[$i]));
        }
    return relleno($binario).lenMsg($binario);
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Alejandro
Val: 1.634
Plata
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

md5 sin la funcion de php

Publicado por Alejandro (839 intervenciones) el 08/05/2015 20:31:30
  • Alejandro se encuentra ahora conectado en el
  • chat de PHP
Este es mi codigo completo, no se porque 0 > 448 y tampoco porque no me da el resultado.
Ayudaaaaa!!!!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<form method="post">
    <input type="text" name="text" size="100" value="<?echo $_POST[text]?>" />
    <input type="submit" value="Hash"/>
</form>
<?
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 "&nbsp;";
        }
        if ($i%80 == 0 & $i > 0){
            echo "\r\n";
        }
    }
    echo strlen($binario)." Bits</textarea><br>";
 
    // Funcion LeftRotate
    function leftrotate($num,$times){
        return ($num << $times) or ($num >> (32-$times));
    }
 
    // Operador Not
    function not($num){
        return pow(2,32) -1 -$num;
    }
 
 
        //Preparacion de la tabla "MD5 checksum"
        $r  = array(  0 => 7, 12, 17, 22,  7, 12, 17, 22,  7, 12, 17, 22,  7, 12, 17, 22 );
        $r += array( 16 => 5,  9, 14, 20,  5,  9, 14, 20,  5,  9, 14, 20,  5,  9, 14, 20 );
        $r += array( 33 => 4, 11, 16, 23,  4, 11, 16, 23,  4, 11, 16, 23,  4, 11, 16, 23 );
        $r += array( 49 => 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)));
        }
 
        //Inicializacion de variables
        $h0 = 0x01234567; //0x67452301
        $h1 = 0x89abcdef; //0xefcdab89
        $h2 = 0xfedcba98; //0x98badcfe
        $h3 = 0x76543210; //0x10325476
 
        //Raunds MD5
        //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
                    $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($bloque);
                $chars = $longitud / 8;
                $hexdecimal="";
                for ($i=1; $i <= $longitud; $i+=8){
                    $char = substr($bloque,$i-1,8);
                    $a = $array[substr($char,0,4)];
                    $b = $array[substr($char,4,4)];
                    $hexdecimal .= "$a$b";
                }
                $longitud = strlen($hexdecimal);
                $chars = $longitud / 8;
                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>";
 
 
            $a = $h0;
            $b = $h1;
            $c = $h2;
            $d = $h3;
            for ($i = 0; $i<63; $i++){
                switch($i){
                    case (0 <= $i and $i <= 15):
                        $f = ($b and $c) or (not($b) and $d);
                        $g = $i;
                    break;
                    case (16 <= $i and $i <= 31):
                        $f = ($d and $b) or (not($d) and $c);
                        $g = (5 * $i + 1) % 16;
                    break;
                    case (32 <= $i and $i <= 47):
                        $f = $b xor $c xor $d;
                        $g = (3 * $i + 5) % 16;
                    break;
                    case (48 <= $i and $i <= 63):
                        $f = $c xor ($b or not($d));
                        $g = (7 * $i) % 16;
                    break;
                }
                $temp = $d;
                $d = $c;
                $c = $b;
                $b = $b + leftrotate($a + $f + $k[$i] + $w[$g],$r[$i]);
                $a = $temp;
            }
            $h0 += $a;
            $h1 += $b;
            $h2 += $c;
            $h3 += $d;
        }
        }
 
        return DECHEX($h0).DECHEX($h1).DECHEX($h2).DECHEX($h3);
        //return revOrd(DECHEX($h0)).revOrd(DECHEX($h1)).revOrd(DECHEX($h2)).revOrd(DECHEX($h3));
}
 
    $text=$_POST[text];
    $checkSum=md5($text);
 
    echo "El Hash de \"<b>$text</b>\" deve ser: <b>$checkSum</b><br>";
 
    $test=my_md5($text);
    echo "Resultado: <b>$test</b><br>";
?>
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Alejandro
Val: 1.634
Plata
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

md5 sin la funcion de php

Publicado por Alejandro (839 intervenciones) el 14/05/2015 01:12:14
  • Alejandro se encuentra ahora conectado en el
  • chat de PHP
Ya obtengo un resultado de 32 caracteres y no repetido como el anterior pero sigue sin ser el correcto al hash md5, ¿En que ando mal? este es mi mas reciente codigo.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<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 "&nbsp;";
        }
        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";
?>
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar