PhpSnip.com

User Stats

SHA-1

A PHP implementation of the Secure Hash Algorithm, SHA-1, based on the JavaScript implementation by Paul Johnston. This is basically a "translation from JavaScript to PHP, so most of the credits should go to Paul Johnston. I only re-wrote it in PHP. See http://pajhome.org.uk/site/legal.html for details.

Info

 Download  View Source (print view)
 Rating : 4.7  Views : 583

Source Code ( 128 lines )

<?php
	/*
	 * sha-1.php
	 * A PHP implementation of the Secure Hash Algorithm, SHA-1, based on
	 * the JavaScript implementation by Paul Johnston.
	 * This is basically a "translation from JavaScript to PHP, so most
	 * of the credits should go to Paul Johnston. I only re-wrote it in PHP.
	 * See http://pajhome.org.uk/site/legal.html for details.
	 */
	
	/*
	 * Convert a 32-bit number to a hex string with ms-byte first
	 */
	function hex($num)
	{
		$hex_chr = "0123456789abcdef";
		$str = "";
		for($j = 7; $j >= 0; $j--)
		$str .= $hex_chr{(($num >> ($j * 4)) & 0x0F)};
		return $str;
	}
	
	/*
	 * Convert a string to a sequence of 16-word blocks, stored as an array.
	 * Append padding bits and the length, as described in the SHA1 standard.
	 */
	function str2blks_SHA1($str)
	{
		$nblk = ((strlen($str) + 8) >> 6) + 1;
		for($i = 0; $i < $nblk * 16; $i++) $blks[$i] = 0;
		for($i = 0; $i < strlen($str); $i++)
			$blks[$i >> 2] |= ord($str{$i}) << (24 - ($i % 4) * 8);
		$blks[$i >> 2] |= 0x80 << (24 - ($i % 4) * 8);
		$blks[$nblk * 16 - 1] = strlen($str) * 8;
		return $blks;
	}
	
	/*
	 * Bitwise rotate a 32-bit number to the left
	 */
	 
	// zeroFill() is needed because PHP doesn't have a zero-fill
	// right shift operator like JavaScript's >>>
	function zeroFill($a, $b)
	{
		$z = hexdec(80000000);
		if ($z & $a)
		{
			$a >>= 1;
			$a &= (~$z);
			$a |= 0x40000000;
			$a >>= ($b-1);
		}
		else
		{
			$a >>= $b;
		}
		return $a;
	}

	function rol($num, $cnt)
	{
		return ($num << $cnt) | (zeroFill($num, (32 - $cnt)));
	}
	
	/*
	 * Perform the appropriate triplet combination function for the current
	 * iteration
	 */
	function ft($t, $b, $c, $d)
	{
	  if($t < 20) return ($b & $c) | ((~$b) & $d);
	  if($t < 40) return $b ^ $c ^ $d;
	  if($t < 60) return ($b & $c) | ($b & $d) | ($c & $d);
	  return $b ^ $c ^ $d;
	}
	
	/*
	 * Determine the appropriate additive constant for the current iteration
	 */
	function kt($t)
	{
	  return ($t < 20) ?  1518500249 : ( ($t < 40) ?  1859775393 : ( ($t < 60) ? -1894007588 : -899497514 ) );
	}
	
	/*
	 * Take a string and return the hex representation of its SHA-1.
	 */
	function calcSHA1($str)
	{
		$x = str2blks_SHA1($str);
				
		$a =  1732584193;
		$b = -271733879;
		$c = -1732584194;
		$d =  271733878;
		$e = -1009589776;
	
		for($i = 0; $i < count($x); $i += 16)
		{
			$olda = $a;
			$oldb = $b;
			$oldc = $c;
			$oldd = $d;
			$olde = $e;

			for($j = 0; $j < 80; $j++)
			{
				if($j < 16) $w[$j] = $x[$i + $j];
				else $w[$j] = rol($w[$j-3] ^ $w[$j-8] ^ $w[$j-14] ^ $w[$j-16], 1);
				
				$t = rol($a, 5) + ft($j, $b, $c, $d) + $e + $w[$j] + kt($j);
				$e = $d;
				$d = $c;
				$c = rol($b, 30);
				$b = $a;
				$a = $t;
			}

			$a += $olda;
			$b += $oldb;
			$c += $oldc;
			$d += $oldd;
			$e += $olde;
		}
		return hex($a) . hex($b) . hex($c) . hex($d) . hex($e);
	}
?>

Search

Subscribe

  Rss Feeds

Sponsors

Advertise