includes/diffiehellman.php
changeset 1261 beb0da036222
parent 1227 bdac73ed481e
equal deleted inserted replaced
1260:cd72bcd83401 1261:beb0da036222
   107 		$lsw = ($x & 0xFFFF) + ($y & 0xFFFF);
   107 		$lsw = ($x & 0xFFFF) + ($y & 0xFFFF);
   108 		$msw = ($x >> 16) + ($y >> 16) + ($lsw >> 16);
   108 		$msw = ($x >> 16) + ($y >> 16) + ($lsw >> 16);
   109 		// 2009-07-02 Added & 0xFFFFFFFF here to fix problem on PHP w/ native 64-bit integer support (rev. 1030)
   109 		// 2009-07-02 Added & 0xFFFFFFFF here to fix problem on PHP w/ native 64-bit integer support (rev. 1030)
   110 		return (($msw << 16) | ($lsw & 0xFFFF)) & 0xFFFFFFFF;
   110 		return (($msw << 16) | ($lsw & 0xFFFF)) & 0xFFFFFFFF;
   111 	}
   111 	}
   112 	function rshz($X, $n)
   112 	function rshz($a, $b)
   113 	{
   113 	{
   114 		// equivalent to $X >>> $n in javascript
   114 		// equivalent to $X >>> $n in javascript
   115 		// pulled from http://www.tapouillo.com/firefox_extension/sourcecode.txt, public domain
   115 		// pulled from http://www.tapouillo.com/firefox_extension/sourcecode.txt, public domain
   116 		$z = hexdec(80000000); 
   116 		$z = hexdec(80000000);
   117 		if ($z & $X) 
   117 		if ($z & $a)
   118 		{ 
   118 		{
   119 				$X = ($X>>1); 
   119 			$a = ($a>>1);
   120 				$X &= (~$z); 
   120 			$a &= (~$z);
   121 				$X |= 0x40000000; 
   121 			$a |= 0x40000000;
   122 				$X = ($X>>($n-1)); 
   122 			$a = ($a>>($b-1));
   123 		} 
   123 		}
   124 		else 
   124 		else
   125 		{ 
   125 		{
   126 				$X = ($X>>$n); 
   126 			$a = ($a>>$b);
   127 		} 
   127 		}
   128 		return $X; 
   128 		return $a;
   129 	}
   129 	}
   130 	function S ($X, $n) {return ( $this->rshz($X, $n) ) | ($X << (32 - $n));}
   130 	function S ($X, $n) {return ( $this->rshz($X, $n) ) | ($X << (32 - $n));}
   131 	function R ($X, $n) {return ( $this->rshz($X, $n) );}
   131 	function R ($X, $n) {return ( $this->rshz($X, $n) );}
   132 	function Ch($x, $y, $z)  {return (($x & $y) ^ ((~$x) & $z));}
   132 	function Ch($x, $y, $z)  {return (($x & $y) ^ ((~$x) & $z));}
   133 	function Maj($x, $y, $z) {return (($x & $y) ^ ($x & $z) ^ ($y & $z));}
   133 	function Maj($x, $y, $z) {return (($x & $y) ^ ($x & $z) ^ ($y & $z));}
   247 			$this->core_sha256(
   247 			$this->core_sha256(
   248 				$this->str2binb($s),
   248 				$this->str2binb($s),
   249 				strlen($s) * $this->chrsz)
   249 				strlen($s) * $this->chrsz)
   250 			);
   250 			);
   251 	}
   251 	}
       
   252 	
       
   253 	/* self-test - make sure PHP isn't screwing us over */
       
   254 	function self_test()
       
   255 	{
       
   256 		return $this->hex_sha256("message digest") == "f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650";
       
   257 	}
       
   258 	
       
   259 	function __construct()
       
   260 	{
       
   261 		if ( !$this->self_test() )
       
   262 			die("SHA256 self test failed.<br />
       
   263 				 Please update PHP to v5.1.2 or later, which includes sha256 support built in. Nothing we can do, your platform likely does not support 32-bit unsigned integers in PHP.");
       
   264 	}
       
   265 }
       
   266 
       
   267 if ( !function_exists('sha256') && function_exists('hash') )
       
   268 {
       
   269 	if ( in_array('sha256', hash_algos()) )
       
   270 	{
       
   271 		// PHP >= 5.1.2 hash support
       
   272 		function sha256($text)
       
   273 		{
       
   274 			return hash('sha256', $text);
       
   275 		}
       
   276 	}
   252 }
   277 }
   253 
   278 
   254 if ( !function_exists('sha256') )
   279 if ( !function_exists('sha256') )
   255 {
   280 {
   256 	function sha256($text)
   281 	function sha256($text)