[pLog-svn] r1957 - plugins/trunk/atom/class/data

oscar at devel.plogworld.net oscar at devel.plogworld.net
Fri May 6 21:35:02 GMT 2005


Author: oscar
Date: 2005-05-06 21:35:01 +0000 (Fri, 06 May 2005)
New Revision: 1957

Modified:
   plugins/trunk/atom/class/data/sha1.class.php
Log:
updated the version of the class that calculates SHA1 digests.

Modified: plugins/trunk/atom/class/data/sha1.class.php
===================================================================
--- plugins/trunk/atom/class/data/sha1.class.php	2005-05-06 16:59:06 UTC (rev 1956)
+++ plugins/trunk/atom/class/data/sha1.class.php	2005-05-06 21:35:01 UTC (rev 1957)
@@ -10,122 +10,94 @@
 
     class SHA1 {	
     
-		/*
-		 * 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) | (SHA1::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 SHA1($str)
-		{
-			$x = SHA1::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] = SHA1::rol($w[$j-3] ^ $w[$j-8] ^ $w[$j-14] ^ $w[$j-16], 1);
-					
-					$t = SHA1::rol($a, 5) + SHA1::ft($j, $b, $c, $d) + $e + $w[$j] + SHA1::kt($j);
-					$e = $d;
-					$d = $c;
-					$c = SHA1::rol($b, 30);
-					$b = $a;
-					$a = $t;
-				}
-				
-				$a += $olda;
-				$b += $oldb;
-				$c += $oldc;
-				$d += $oldd;
-				$e += $olde;
-			}
-			return SHA1::hex($a) . SHA1::hex($b) . SHA1::hex($c) . SHA1::hex($d) . SHA1::hex($e);
-		}
+        /* 
+        ** PHP implementation of the Secure Hash Algorithm, SHA-1, as defined 
+        ** in FIPS PUB 180-1 
+        * 
+        ** Version 1.1 
+        ** Copyright 2002 - 2003 Marcus Campbell 
+        ** http://www.tecknik.net/sha-1/ 
+        * 
+        ** This code is available under the GNU Lesser General Public License: 
+        ** http://www.gnu.org/licenses/lgpl.txt 
+        * 
+        ** Based on the JavaScript implementation by Paul Johnston 
+        ** http://pajhome.org.uk/ 
+        */ 
+        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(substr($str, $i, 1)) << (24 - ($i % 4) * 8); 
+            } 
+            $blks[$i >> 2] |= 0x80 << (24 - ($i % 4) * 8); 
+            $blks[$nblk * 16 - 1] = strlen($str) * 8; 
+            return $blks; 
+        } 
+        function safe_add($x, $y) { 
+            $lsw = ($x & 0xFFFF) + ($y & 0xFFFF); 
+            $msw = ($x >> 16) + ($y >> 16) + ($lsw >> 16); 
+            return ($msw << 16) | ($lsw & 0xFFFF); 
+        } 
+        function rol($num, $cnt) { 
+            return ($num << $cnt) | SHA1::zeroFill($num, 32 - $cnt); 
+        } 
+        function zeroFill($a, $b) { 
+            $bin = decbin($a); 
+            if (strlen($bin) < $b) $bin = 0; 
+            else $bin = substr($bin, 0, strlen($bin) - $b); 
+            for ($i=0; $i < $b; $i++) { 
+                $bin = "0".$bin; 
+            } 
+            return bindec($bin); 
+        } 
+        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; 
+        } 
+        function kt($t) { 
+            if ($t < 20) { 
+                return 1518500249; 
+            } else if ($t < 40) { 
+                return 1859775393; 
+            } else if ($t < 60) { 
+                return -1894007588; 
+            } else { 
+                return -899497514; 
+            } 
+        } 
+        function sha1($str) { 
+            $x = SHA1::str2blks_SHA1($str); 
+            $a =  1732584193; 
+            $b = -271733879; 
+            $c = -1732584194; 
+            $d =  271733878; 
+            $e = -1009589776; 
+            for($i = 0; $i < sizeof($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] = SHA1::rol($w[$j - 3] ^ $w[$j - 8] ^ $w[$j - 14] ^ $w[$j - 16], 1); 
+                    $t = SHA1::safe_add(SHA1::safe_add(SHA1::rol($a, 5), SHA1::ft($j, $b, $c, $d)), SHA1::safe_add(SHA1::safe_add($e, $w[$j]), SHA1::kt($j))); 
+                    $e = $d; 
+                    $d = $c; 
+                    $c = SHA1::rol($b, 30); 
+                    $b = $a; 
+                    $a = $t; 
+                } 
+                $a = SHA1::safe_add($a, $olda); 
+                $b = SHA1::safe_add($b, $oldb); 
+                $c = SHA1::safe_add($c, $oldc); 
+                $d = SHA1::safe_add($d, $oldd); 
+                $e = SHA1::safe_add($e, $olde); 
+            } 
+            return sprintf("%08s%08s%08s%08s%08s", dechex($a), dechex($b), dechex($c), dechex($d), dechex($e)); 
+        }    
     }
 ?>




More information about the pLog-svn mailing list