I have tried a new code to implement the Sha1 algorithm but still I could not succeed in it.
Could any one help me find out where i have done the mistake?
the wikipedia
link
the rfc
link
my page at
work
Supply an input string in the text box and you will see the debug statements printed on the screen; i have tried my best to make it as easy as possible for you to understand my code.
thanks for any help.
Here is the source code of the file:
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>SHA 1 Hashing</title>
</head>
<body>
<form name="form_encryption" method="get" action="">
<input type="text" value="" name="input_string" onblur="this.form.submit();" />
</form>
</body>
</html>
<?php
/* set parameters */
error_reporting(E_ALL);
/* encrypt if initial conditions are met */
if (isset($_GET["input_string"])){
/* initialize variables */
$input_string_ascii_array = array();
$input_string_binary_array = array();
$binary_512_chunks_array = array();
$binary_32_chunks_array = array();
/* Get the input varible from the $_GET array */
$input_string = $_GET["input_string"];
/* define initial variables */
$h0 = "0x67452301";
$h1 = "0xEFCDAB89";
$h2 = "0x98BADCFE";
$h3 = "0x10325476";
$h4 = "0xC3D2E1F0";
echo "<br /><br />"."<---------------Start of processing input string to binary and appending 1 to it----------------->";
/* convert and obtain the input string in ASCII chars */
$input_string_ascii_array = change_string_to_ascii($input_string);
echo "<br /><br />"."Main block- Original input binary string:"; print_r($input_string_ascii_array);
/* convert and obtain the ASCII chars as Binary array */
$input_string_binary_array = change_ascii_to_binary($input_string_ascii_array);
echo "-----------received binary array------------";
print_r($input_string_binary_array);
$input_string_binary = implode("", $input_string_binary_array);
/* pad an additional one bit, the value one to the existing binary input string */
$input_string_binary .= '1';
echo "<br /><br />"."Main block- After appending to the original input binary string:".$input_string_binary;
echo "<br /><br />"."<------------End of processing input string to binary and appending 1 to it-------------->";
/* make the string as 512 bits chunk; else pad zero to make it 512 bit chunks */
$input_string_binary = make_512_bits_chunks($input_string_binary);
/* get the input binary string as array of individual 512 bits chunks */
$binary_512_chunks_array = get_array_n_bits_chunks($input_string_binary, 512);
/* split each 512 bits chunk into 32 bit chunks array */
$binary_32_chunks_array = get_array_n_bits_chunks($input_string_binary, 32);
/* extend 16 32-bits chunks into 80(0 - 79) 32-bits chunks */
for ($i = 16; $i <= 79; $i++){
$binary_32_chunks_array[$i] = decbin((bindec($binary_32_chunks_array[$i-3]) ^ bindec($binary_32_chunks_array[$i-8]) ^ bindec($binary_32_chunks_array[$i-14]) ^ bindec($binary_32_chunks_array[$i-16])) << 1);
}
echo "<br /><br /><---------Main block - Start of displaying 80 32bits chunks:----------><br />";
print_r($binary_32_chunks_array);
echo "<br /><----------Main block - End of displaying 80 32bits chunks:----------><br />";
/* Initialize hash value for this chunk */
$a = $h0;
$b = $h1;
$c = $h2;
$d = $h3;
$e = $h4;
/* Generating a, b, c, d, e, f and k values; Bitwise operations performed in decimals */
echo "<br /><----------Main block - Start of generating f and k values----------><br />";
for ($i = 0; $i <= 79; $i++){
if ($i >= 0 && $i <= 19){
$f = (hexdec($b) & hexdec($c)) | (~hexdec($b) & hexdec($d));
$k = "0x5A827999";
}elseif($i >= 20 && $i <= 39){
$f = hexdec($b) ^ hexdec($c) ^ hexdec($d);
$k = "0x6ED9EBA1";
}elseif($i >= 40 && $i <= 59){
$f = (hexdec($b) & hexdec($c)) or (hexdec($b) & hexdec($d)) or (hexdec($c) & hexdec($d));
$k = "0x8F1BBCDC";
}elseif($i >= 60 && $i <= 79){
$f = hexdec($b) ^ hexdec($c) ^ hexdec($d);
$k = "0xCA62C1D6";
}
echo "<br /><br />Main Block - f and k values: $i: $f, $k";
echo "<br />Main Block: a, b, c, d, e, f, k: $a, $b, $c, $d, $e, $f, $k";
/* add all the variables in the next statement in decimals and convert them back to hex */
$temp = (hexdec($a) << 5) + $f + $e + hexdec($k) + bindec($binary_32_chunks_array[$i]);
$e = $d;
$d = $c;
$c = hexdec($b) << 30;
$b = $a;
$a = $temp;
echo "<br />Main Block - After final assignments: a, b, c, d, e, f, k: $a, $b, $c, $d, $e, $f, $k";
}
echo "<br /><----------Main block - End of generating a, b, c, d, e, f and k values----------><br />";
//Add this chunk hash to the result so far
echo "<br /><br />"."h0, h1, h2, h3 & h4:$h0, $h1, $h2, $h3, $h4";
$h0 = dechex(hexdec($h0) + $a);
$h1 = dechex(hexdec($h1) + $b);
$h2 = dechex(hexdec($h2) + $c);
$h3 = dechex(hexdec($h3) + $d);
$h4 = dechex(hexdec($h4) + $e);
$digest = $h0.$h1.$h2.$h3.$h4;
echo "<br /><br />"."Final digest generated:".$digest;
echo "<br />"."Php sha1 value:".sha1($_GET["input_string"]);
}
?>
<?php
function change_ascii_to_binary($input_string_ascii_array){
/* initialize an array where all the converted ASCII chars are stored*/
$input_string_binary_array = array();
/* count the number of elements in the array*/
$input_string_ascii_length = count($input_string_ascii_array);
/* convert the input string in ASCII(Decimal) to binary data */
for($i = 0; $i < $input_string_ascii_length; $i++){
array_push($input_string_binary_array, pad_leading_zeros(base_convert($input_string_ascii_array[$i], 10, 2), );
}
echo "<br /><br />change_ascii_to_binary-input string in binary format:"; print_r($input_string_binary_array);
return $input_string_binary_array;
}
?>
<?php
function change_string_to_ascii($input_string){
/* initialize an array where all the converted ASCII chars are stored*/
$input_string_ascii_array = array();
/* find the length of the input string */
$input_len = strlen($input_string);
/* loop and extract every character of the input string and convert to ASCII */
for($i = 0; $i < $input_len; $i++){
##DEBUG##echo "<br />"."Print ASCII chars:<br />".ord($input_string{$i})."<br />";
array_push($input_string_ascii_array, ord($input_string{$i}));
}
##DEBUG##echo "<br />changeStringToAscii-Input as ASCII:"; print_r($input_string_ascii_array);
return $input_string_ascii_array;
}
?>
<?php
function pad_leading_zeros($input_binary, $output_string_binary_length){
##DEBUG##echo "Input binary:".$input_binary."<br />";
/* input element index used to navigate characters */
$input_index = 0;
/* New binary string which is going to returned */
$output_binary_array = array();
/* find the length of the binary input */
$input_binary_length = strlen($input_binary);
/* find the number of zeros to be padded to the binary character */
$pad_zeros_length = $output_string_binary_length - $input_binary_length;
for ($i = 0; $i < $output_string_binary_length; $i++){
if ($i < $pad_zeros_length){
$output_binary_array[$i] = "0";
}else{
$input_index = $i - $pad_zeros_length;
$output_binary_array[$i] = $input_binary{$input_index};
}
##DEBUG##echo "<br />padLeadingZeros - Leading zeros padded string:"; print_r($output_binary_array);
}
##DEBUG##echo "<br />"."binary values imploded:".implode("", $output_binary_array);
return implode("", $output_binary_array);
}
?>
<?php
function make_512_bits_chunks($input_string_binary){
echo "<br /><br />"."<------------ Start of 512 bits processing --------------->";
/* initialize variables */
$output_string_binary = $input_string_binary;
/* find the length of the input binary string */
$input_string_length_binary = strlen($input_string_binary);
echo "<br />"."make_512_bits_chunks: length of the input binary string: ".$input_string_length_binary;
/* convert the length of the input string in binary */
$input_string_length_binary = base_convert($input_string_length_binary, 10, 2);
echo "<br />"."make_512_bits_chunks: length of the input binary string in bits: ".$input_string_length_binary;
/* Pad the length of the input string in binary with leading zeros to make the length as 64 bits */
$input_string_length_binary = pad_leading_zeros($input_string_length_binary, 64);
echo "<br />"."make_512_bits_chunks: length of the input binary string in 64 bits: ".$input_string_length_binary;
/* find number of zeros to be padded */
$input_string_binary_length = strlen($input_string_binary);
if ($input_string_binary_length < 512){
$pad_zeros_length = (($input_string_binary_length < 448)?(448-$input_string_binary_length):(448- ($input_string_binary_length % 448)));
}
else{
/*
1. find the integer value when dividing the input string length by 512
2. multiply the 512 by the above value + 448 which should be the required length of the input string
*/
$temp = intval($input_string_binary_length / 512);
echo "<br />"."make_512_bits_chunks: input binary string length/512: ".$temp;
$pad_zeros_length = ((512 * $temp) + 448) - $input_string_binary_length;
}
echo "<br />"."make_512_bits_chunks: number of zeros to be padded: ".$pad_zeros_length;
/* pad trailing zeros to the input binary string to make the length of string as multiples of 448*/
for ($i = 0; $i < abs($pad_zeros_length); $i++){
$output_string_binary .= '0';
}
echo "<br />"."make_512_bits_chunks: after padding zeros to be binary string to make it multiples of 448: ".$output_string_binary;
/* append the length of the string in 64 bits with the input string in binary and in multiples in 448 */
$output_string_binary .= $input_string_length_binary;
echo "<br />"."make_512_bits_chunks: Final binary string: ".$output_string_binary;
echo "<br />"."make_512_bits_chunks: Length of the binary string: ".strlen($output_string_binary);
echo "<br /><br />"."<------------ End of 512 bits processing --------------->";
return $output_string_binary;
}
?>
<?php
function get_array_n_bits_chunks($input_string_binary, $n){
/* initialize variables */
$binary_n_bits_chunks_array = array();
/* find the number of 512 chunks available */
$input_string_binary_chunks = strlen($input_string_binary) / $n ;
/* look and separate 'n' bits chunks and put them in array */
for ($i = 0; $i < $input_string_binary_chunks; $i++){
array_push($binary_n_bits_chunks_array, substr($input_string_binary, $i * $n, $n));
}
##DEBUG##echo "<br />get_array_$n_bits_chunks:"; print_r($binary_n_bits_chunks_array);
return $binary_n_bits_chunks_array;
}
?>