class_CryptoHelper.php

Go to the documentation of this file.
00001 <?php
00024 class CryptoHelper extends BaseFrameworkSystem implements Cryptable {
00025         // Exception constants
00026         const EXCEPTION_ENCRYPT_MISSING = 0x1f0;
00027         const EXCEPTION_ENCRYPT_INVALID = 0x1f1;
00028 
00032         private static $selfInstance = null;
00033 
00037         private $rngInstance = null;
00038 
00042         private $salt = "";
00043 
00049         protected function __construct () {
00050                 // Call parent constructor
00051                 parent::__construct(__CLASS__);
00052 
00053                 // Clean up a little
00054                 $this->removeNumberFormaters();
00055                 $this->removeSystemArray();
00056         }
00057 
00063         public final static function createCryptoHelper () {
00064                 // Get a new instance
00065                 $cryptoInstance = new CryptoHelper();
00066 
00067                 // Initialize the hasher
00068                 $cryptoInstance->initHasher();
00069 
00070                 // Return the instance
00071                 return $cryptoInstance;
00072         }
00073 
00079         public final static function getInstance () {
00080                 // Is no instance there?
00081                 if (is_null(self::$selfInstance)) {
00082                         // Then get a new one
00083                         self::$selfInstance = self::createCryptoHelper();
00084                 }
00085 
00086                 // Return the instance
00087                 return self::$selfInstance;
00088         }
00089 
00095         protected function initHasher () {
00096                 // Initialize the random number generator which is required by some crypto methods
00097                 $this->rngInstance = ObjectFactory::createObjectByConfiguredName('rng_class');
00098 
00099                 // Generate a salt for the hasher
00100                 $this->generateSalt();
00101         }
00102 
00108         private function generateSalt () {
00109                 // Get a random string from the RNG
00110                 $randomString = $this->rngInstance->randomString();
00111 
00112                 // Get config entry for salt length
00113                 $length = $this->getConfigInstance()->readConfig('salt_length');
00114 
00115                 // Keep only defined number of characters
00116                 $this->salt = substr(sha1($randomString), -$length, $length);
00117         }
00118 
00129         public function hashString ($str, $oldHash = "") {
00130                 // Cast the string
00131                 $str = (string) $str;
00132 
00133                 // Default is the default salt ;-)
00134                 $salt = $this->salt;
00135 
00136                 // Is the old password set?
00137                 if (!empty($oldHash)) {
00138                         // Use the salt from hash, first get length
00139                         $length = $this->getConfigInstance()->readConfig('salt_length');
00140 
00141                         // Then extract the X first characters from the hash as our salt
00142                         $salt = substr($oldHash, 0, $length);
00143                 } // END - if
00144 
00145                 // Hash the password with salt
00146                 //* DEBUG: */ echo "salt=".$salt."/plain=".$str."<br />\n";
00147                 $hashed = $salt . md5(sprintf($this->getConfigInstance()->readConfig('hash_mask'),
00148                         $salt,
00149                         $this->rngInstance->getFixedSalt(),
00150                         $str
00151                 ));
00152 
00153                 // And return it
00154                 return $hashed;
00155         }
00156 
00163         public function encryptString ($str) {
00164                 // Init crypto module
00165                 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
00166                 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
00167 
00168                 // Get key
00169                 if ($this->getConfigInstance()->readConfig('crypt_fixed_salt') === "Y") {
00170                         $key = md5($this->rngInstance->getFixedSalt());
00171                 } else {
00172                         $key = md5($this->rngInstance->getExtraSalt());
00173                 }
00174 
00175                 // Add some "garbage" to the string
00176                 switch ($this->rngInstance->randomNumber(0, 8)) {
00177                         case 0:
00178                                 $garbageString = crc32($this->rngInstance->randomString(10))."|".base64_encode($str)."|".crc32($this->rngInstance->randomString(20));
00179                                 break;
00180 
00181                         case 1:
00182                                 $garbageString = crc32($this->rngInstance->randomString(10))."|".base64_encode($str)."|".md5($this->rngInstance->randomString(20));
00183                                 break;
00184 
00185                         case 2:
00186                                 $garbageString = crc32($this->rngInstance->randomString(10))."|".base64_encode($str)."|".sha1($this->rngInstance->randomString(20));
00187                                 break;
00188 
00189                         case 3:
00190                                 $garbageString = md5($this->rngInstance->randomString(10))."|".base64_encode($str)."|".crc32($this->rngInstance->randomString(20));
00191                                 break;
00192 
00193                         case 4:
00194                                 $garbageString = md5($this->rngInstance->randomString(10))."|".base64_encode($str)."|".md5($this->rngInstance->randomString(20));
00195                                 break;
00196 
00197                         case 5:
00198                                 $garbageString = md5($this->rngInstance->randomString(10))."|".base64_encode($str)."|".sha1($this->rngInstance->randomString(20));
00199                                 break;
00200 
00201                         case 6:
00202                                 $garbageString = sha1($this->rngInstance->randomString(10))."|".base64_encode($str)."|".crc32($this->rngInstance->randomString(20));
00203                                 break;
00204 
00205                         case 7:
00206                                 $garbageString = sha1($this->rngInstance->randomString(10))."|".base64_encode($str)."|".md5($this->rngInstance->randomString(20));
00207                                 break;
00208 
00209                         case 8:
00210                                 $garbageString = sha1($this->rngInstance->randomString(10))."|".base64_encode($str)."|".sha1($this->rngInstance->randomString(20));
00211                                 break;
00212                 }
00213 
00214                 // Encrypt the string
00215                 $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $garbageString, MCRYPT_MODE_ECB, $iv);
00216 
00217                 // Return the string
00218                 return $encrypted;
00219         }
00220 
00227         public function decryptString ($encrypted) {
00228                 // Init crypto module
00229                 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
00230                 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
00231 
00232                 // Get key
00233                 if ($this->getConfigInstance()->readConfig('crypt_fixed_salt') === "Y") {
00234                         $key = md5($this->rngInstance->getFixedSalt());
00235                 } else {
00236                         $key = md5($this->rngInstance->getExtraSalt());
00237                 }
00238 
00239                 // Decrypt the string
00240                 $garbageString = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB, $iv);
00241 
00242                 // Get the real string out
00243                 $strArray = explode("|", $garbageString);
00244 
00245                 // Does the element count match?
00246                 assert(count($strArray) == 3);
00247 
00248                 // Decode the string
00249                 $str = base64_decode($strArray[1]);
00250 
00251                 // Trim trailing nulls away
00252                 $str = rtrim($str, "\0");
00253 
00254                 // Return the string
00255                 return $str;
00256         }
00257 }
00258 
00259 // [EOF]
00260 ?>

Generated on Mon Dec 8 01:06:45 2008 for Ship-Simulator by  doxygen 1.5.6