X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Fcrypto%2Fclass_CryptoHelper.php;h=95e31175644c74af361b2a9461969e4868160d0e;hp=34480b49343afd8e52472a48688a13b5a59c074b;hb=84e2207412d3c6ea9f940a83b2cdd4503509808a;hpb=b9c18d6c24e3be4393bf41005aa4e428a0ea3218 diff --git a/inc/classes/main/crypto/class_CryptoHelper.php b/inc/classes/main/crypto/class_CryptoHelper.php index 34480b49..95e31175 100644 --- a/inc/classes/main/crypto/class_CryptoHelper.php +++ b/inc/classes/main/crypto/class_CryptoHelper.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * @@ -37,14 +37,14 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable { private $rngInstance = null; /** - * Salt for hashing operations + * Instance of the crypto stream */ - private $salt = ''; + private $cryptoStreamInstance = null; /** - * Seperator on many places + * Salt for hashing operations */ - private $seperator = '|'; + private $salt = ''; /** * Protected constructor @@ -54,10 +54,6 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable { protected function __construct () { // Call parent constructor parent::__construct(__CLASS__); - - // Clean up a little - $this->removeNumberFormaters(); - $this->removeSystemArray(); } /** @@ -72,6 +68,9 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable { // Initialize the hasher $cryptoInstance->initHasher(); + // Attach a crypto stream + $cryptoInstance->attachCryptoStream(); + // Return the instance return $cryptoInstance; } @@ -86,12 +85,29 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable { if (is_null(self::$selfInstance)) { // Then get a new one self::$selfInstance = self::createCryptoHelper(); - } + } // END - if // Return the instance return self::$selfInstance; } + /** + * Attaches a crypto stream to this crypto helper by detecting loaded + * modules. + * + * @return void + */ + protected function attachCryptoStream () { + // Do we have mcrypt loaded? + if ($this->isPhpExtensionLoaded('mcrypt')) { + // Then use it + $this->cryptoStreamInstance = ObjectFactory::createObjectByName('McryptStream', array($this->getRngInstance())); + } else { + // If nothing works ... + $this->cryptoStreamInstance = ObjectFactory::createObjectByName('NullCryptoStream'); + } + } + /** * Initializes the hasher for different purposes. * @@ -99,7 +115,7 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable { */ protected function initHasher () { // Initialize the random number generator which is required by some crypto methods - $this->rngInstance = ObjectFactory::createObjectByConfiguredName('rng_class'); + $this->setRngInstance(ObjectFactory::createObjectByConfiguredName('rng_class')); // Generate a salt for the hasher $this->generateSalt(); @@ -112,7 +128,7 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable { */ private function generateSalt () { // Get a random string from the RNG - $randomString = $this->rngInstance->randomString(); + $randomString = $this->getRngInstance()->randomString(); // Get config entry for salt length $length = $this->getConfigInstance()->getConfigEntry('salt_length'); @@ -151,7 +167,7 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable { //* DEBUG: */ echo "salt=".$salt."/plain=".$str."
\n"; $hashed = $salt . md5(sprintf($this->getConfigInstance()->getConfigEntry('hash_mask'), $salt, - $this->rngInstance->getFixedSalt(), + $this->getRngInstance()->getFixedSalt(), $str )); @@ -166,58 +182,8 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable { * @return $encrypted Encrypted string */ public function encryptString ($str) { - // Init crypto module - $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); - $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); - - // Get key - if ($this->getConfigInstance()->getConfigEntry('crypt_fixed_salt') == 'Y') { - $key = md5($this->rngInstance->getFixedSalt()); - } else { - $key = md5($this->rngInstance->getExtraSalt()); - } - - // Add some "garbage" to the string - switch ($this->rngInstance->randomNumber(0, 8)) { - case 0: - $garbageString = crc32($this->rngInstance->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . crc32($this->rngInstance->randomString(20)); - break; - - case 1: - $garbageString = crc32($this->rngInstance->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . md5($this->rngInstance->randomString(20)); - break; - - case 2: - $garbageString = crc32($this->rngInstance->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . sha1($this->rngInstance->randomString(20)); - break; - - case 3: - $garbageString = md5($this->rngInstance->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . crc32($this->rngInstance->randomString(20)); - break; - - case 4: - $garbageString = md5($this->rngInstance->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . md5($this->rngInstance->randomString(20)); - break; - - case 5: - $garbageString = md5($this->rngInstance->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . sha1($this->rngInstance->randomString(20)); - break; - - case 6: - $garbageString = sha1($this->rngInstance->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . crc32($this->rngInstance->randomString(20)); - break; - - case 7: - $garbageString = sha1($this->rngInstance->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . md5($this->rngInstance->randomString(20)); - break; - - case 8: - $garbageString = sha1($this->rngInstance->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . sha1($this->rngInstance->randomString(20)); - break; - } - - // Encrypt the string - $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $garbageString, MCRYPT_MODE_ECB, $iv); + // Encrypt the string through the stream + $encrypted = $this->cryptoStreamInstance->encryptStream($str); // Return the string return $encrypted; @@ -230,31 +196,8 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable { * @return $str The unencrypted string */ public function decryptString ($encrypted) { - // Init crypto module - $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); - $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); - - // Get key - if ($this->getConfigInstance()->getConfigEntry('crypt_fixed_salt') == 'Y') { - $key = md5($this->rngInstance->getFixedSalt()); - } else { - $key = md5($this->rngInstance->getExtraSalt()); - } - - // Decrypt the string - $garbageString = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB, $iv); - - // Get the real string out - $strArray = explode($this->seperator, $garbageString); - - // Does the element count match? - assert(count($strArray) == 3); - - // Decode the string - $str = base64_decode($strArray[1]); - - // Trim trailing nulls away - $str = rtrim($str, "\0"); + // Encrypt the string through the stream + $str = $this->cryptoStreamInstance->decryptStream($encrypted); // Return the string return $str;