Sha1.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2008, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * SHA1 Encoding Class
00020  *
00021  * Purpose: Provides 160 bit hashing using The Secure Hash Algorithm
00022  * developed at the National Institute of Standards and Technology. The 40
00023  * character SHA1 message hash is computationally infeasible to crack.
00024  *
00025  * This class is a fallback for servers that are not running PHP greater than
00026  * 4.3, or do not have the MHASH library.
00027  *
00028  * This class is based on two scripts:
00029  *
00030  * Marcus Campbell's PHP implementation (GNU license)
00031  * http://www.tecknik.net/sha-1/
00032  *
00033  * ...which is based on Paul Johnston's JavaScript version
00034  * (BSD license). http://pajhome.org.uk/
00035  *
00036  * I encapsulated the functions and wrote one additional method to fix
00037  * a hex conversion bug. - Rick Ellis
00038  *
00039  * @package             CodeIgniter
00040  * @subpackage  Libraries
00041  * @category    Encryption
00042  * @author              ExpressionEngine Dev Team
00043  * @link                http://codeigniter.com/user_guide/general/encryption.html
00044  */
00045 class CI_SHA {
00046 
00047         function CI_SHA()
00048         {
00049                 log_message('debug', "SHA1 Class Initialized");
00050         }
00051 
00052         /**
00053          * Generate the Hash
00054          *
00055          * @access      public
00056          * @param       string
00057          * @return      string
00058          */     
00059         function generate($str)
00060         {
00061                 $n = ((strlen($str) + 8) >> 6) + 1;
00062 
00063                 for ($i = 0; $i < $n * 16; $i++)
00064                 {
00065                         $x[$i] = 0;
00066                 }
00067 
00068                 for ($i = 0; $i < strlen($str); $i++)
00069                 {
00070                         $x[$i >> 2] |= ord(substr($str, $i, 1)) << (24 - ($i % 4) * 8);
00071                 }
00072 
00073                 $x[$i >> 2] |= 0x80 << (24 - ($i % 4) * 8);
00074 
00075                 $x[$n * 16 - 1] = strlen($str) * 8;
00076 
00077                 $a =  1732584193;
00078                 $b = -271733879;
00079                 $c = -1732584194;
00080                 $d =  271733878;
00081                 $e = -1009589776;
00082 
00083                 for ($i = 0; $i < sizeof($x); $i += 16)
00084                 {
00085                         $olda = $a;
00086                         $oldb = $b;
00087                         $oldc = $c;
00088                         $oldd = $d;
00089                         $olde = $e;
00090 
00091                         for($j = 0; $j < 80; $j++)
00092                         {
00093                                 if ($j < 16)
00094                                 {
00095                                         $w[$j] = $x[$i + $j];
00096                                 }
00097                                 else
00098                                 {
00099                                         $w[$j] = $this->_rol($w[$j - 3] ^ $w[$j - 8] ^ $w[$j - 14] ^ $w[$j - 16], 1);
00100                                 }
00101 
00102                                 $t = $this->_safe_add($this->_safe_add($this->_rol($a, 5), $this->_ft($j, $b, $c, $d)), $this->_safe_add($this->_safe_add($e, $w[$j]), $this->_kt($j)));
00103 
00104                                 $e = $d;
00105                                 $d = $c;
00106                                 $c = $this->_rol($b, 30);
00107                                 $b = $a;
00108                                 $a = $t;
00109                         }
00110 
00111                         $a = $this->_safe_add($a, $olda);
00112                         $b = $this->_safe_add($b, $oldb);
00113                         $c = $this->_safe_add($c, $oldc);
00114                         $d = $this->_safe_add($d, $oldd);
00115                         $e = $this->_safe_add($e, $olde);
00116                 }
00117 
00118                 return $this->_hex($a).$this->_hex($b).$this->_hex($c).$this->_hex($d).$this->_hex($e);
00119         }
00120         
00121         // --------------------------------------------------------------------
00122 
00123         /**
00124          * Convert a decimal to hex
00125          *
00126          * @access      private
00127          * @param       string
00128          * @return      string
00129          */     
00130         function _hex($str)
00131         {
00132                 $str = dechex($str);
00133 
00134                 if (strlen($str) == 7)
00135                 {
00136                         $str = '0'.$str;
00137                 }
00138 
00139                 return $str;
00140         }
00141         
00142         // --------------------------------------------------------------------
00143 
00144         /**
00145          *  Return result based on iteration
00146          *
00147          * @access      private
00148          * @return      string
00149          */     
00150         function _ft($t, $b, $c, $d)
00151         {
00152                 if ($t < 20)
00153                         return ($b & $c) | ((~$b) & $d);
00154                 if ($t < 40)
00155                         return $b ^ $c ^ $d;
00156                 if ($t < 60)
00157                         return ($b & $c) | ($b & $d) | ($c & $d);
00158 
00159                 return $b ^ $c ^ $d;
00160         }
00161 
00162         // --------------------------------------------------------------------
00163 
00164         /**
00165          * Determine the additive constant
00166          *
00167          * @access      private
00168          * @return      string
00169          */     
00170         function _kt($t)
00171         {
00172                 if ($t < 20)
00173                 {
00174                         return 1518500249;
00175                 }
00176                 else if ($t < 40)
00177                 {
00178                         return 1859775393;
00179                 }
00180                 else if ($t < 60)
00181                 {
00182                         return -1894007588;
00183                 }
00184                 else
00185                 {
00186                         return -899497514;
00187                 }
00188         }
00189         
00190         // --------------------------------------------------------------------
00191 
00192         /**
00193          * Add integers, wrapping at 2^32
00194          *
00195          * @access      private
00196          * @return      string
00197          */     
00198         function _safe_add($x, $y)
00199         {
00200                 $lsw = ($x & 0xFFFF) + ($y & 0xFFFF);
00201                 $msw = ($x >> 16) + ($y >> 16) + ($lsw >> 16);
00202 
00203                 return ($msw << 16) | ($lsw & 0xFFFF);
00204         }
00205         
00206         // --------------------------------------------------------------------
00207 
00208         /**
00209          * Bitwise rotate a 32-bit number
00210          *
00211          * @access      private
00212          * @return      integer
00213          */     
00214         function _rol($num, $cnt)
00215         {
00216                 return ($num << $cnt) | $this->_zero_fill($num, 32 - $cnt);
00217         }
00218 
00219         // --------------------------------------------------------------------
00220 
00221         /**
00222          * Pad string with zero
00223          *
00224          * @access      private
00225          * @return      string
00226          */     
00227         function _zero_fill($a, $b)
00228         {
00229                 $bin = decbin($a);
00230 
00231                 if (strlen($bin) < $b)
00232                 {
00233                         $bin = 0;
00234                 }
00235                 else
00236                 {
00237                         $bin = substr($bin, 0, strlen($bin) - $b);
00238                 }
00239 
00240                 for ($i=0; $i < $b; $i++)
00241                 {
00242                         $bin = "0".$bin;
00243                 }
00244 
00245                 return bindec($bin);
00246         }
00247 }
00248 // END CI_SHA
00249 
00250 /* End of file Sha1.php */
00251 /* Location: ./system/libraries/Sha1.php */