X-Git-Url: https://git.mxchange.org/?p=shipsimu.git;a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Fcrypto%2Fclass_CryptoHelper.php;h=1ccdaae901b0712c8644e6001b8de53598cd050d;hp=fc0cd39b806d64da5c7ba365e14d691e060b84bd;hb=29266e780769e83234f586c9e5779e3ebc593264;hpb=fe76c3626828df10434f15c67a613d8f5d27ff20 diff --git a/inc/classes/main/crypto/class_CryptoHelper.php b/inc/classes/main/crypto/class_CryptoHelper.php index fc0cd39..1ccdaae 100644 --- a/inc/classes/main/crypto/class_CryptoHelper.php +++ b/inc/classes/main/crypto/class_CryptoHelper.php @@ -3,10 +3,10 @@ * A helper class for cryptographical things like hashing passwords and so on * * @author Roland Haeder - * @version 0.3.0 - * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, this is free software * @license GNU GPL 3.0 or any newer version - * @link http://www.mxchange.org + * @link http://www.ship-simu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,9 +19,13 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program. If not, see . + * along with this program. If not, see . */ -class CryptoHelper extends BaseFrameworkSystem { +class CryptoHelper extends BaseFrameworkSystem implements Cryptable { + // Exception constants + const EXCEPTION_ENCRYPT_MISSING = 0x1f0; + const EXCEPTION_ENCRYPT_INVALID = 0x1f1; + /** * An instance of this own clas */ @@ -46,12 +50,6 @@ class CryptoHelper extends BaseFrameworkSystem { // Call parent constructor parent::__construct(__CLASS__); - // Set part description - $this->setObjectDescription("Cryptographical helper"); - - // Create unique ID number - $this->createUniqueID(); - // Clean up a little $this->removeNumberFormaters(); $this->removeSystemArray(); @@ -64,7 +62,7 @@ class CryptoHelper extends BaseFrameworkSystem { */ public final static function createCryptoHelper () { // Get a new instance - $cryptoInstance = self::getInstance(); + $cryptoInstance = new CryptoHelper(); // Initialize the hasher $cryptoInstance->initHasher(); @@ -82,7 +80,7 @@ class CryptoHelper extends BaseFrameworkSystem { // Is no instance there? if (is_null(self::$selfInstance)) { // Then get a new one - self::$selfInstance = new CryptoHelper(); + self::$selfInstance = self::createCryptoHelper(); } // Return the instance @@ -109,7 +107,7 @@ class CryptoHelper extends BaseFrameworkSystem { */ private function generateSalt () { // Get a random string from the RNG - $randomString = $this->rngInstance->makeRandomString(); + $randomString = $this->rngInstance->randomString(); // Get config entry for salt length $length = $this->getConfigInstance()->readConfig('salt_length'); @@ -119,38 +117,143 @@ class CryptoHelper extends BaseFrameworkSystem { } /** - * Hashes a password with salt and returns the hash. If an old previous hash + * Hashes a string with salt and returns the hash. If an old previous hash * is supplied the method will use the first X chars of that hash for hashing * the password. This is useful if you want to check if the password is * identical for authorization purposes. * - * @param $plainPassword The plain password to use - * @param $oldHash A previously hashed password - * @return $hashed The hashed and salted password + * @param $str Unhashed string + * @param $oldHash A hash from previous hashed string + * @return $hashed The hashed and salted string */ - public function hashPassword ($plainPassword, $oldHash = "") { + public function hashString ($str, $oldHash = "") { + // Cast the string + $str = (string) $str; + + // Default is the default salt ;-) + $salt = $this->salt; + // Is the old password set? - if (empty($oldHash)) { - // No, then use the current salt - $salt = $this->salt; - } else { + if (!empty($oldHash)) { // Use the salt from hash, first get length $length = $this->getConfigInstance()->readConfig('salt_length'); // Then extract the X first characters from the hash as our salt $salt = substr($oldHash, 0, $length); - } + } // END - if // Hash the password with salt + //* DEBUG: */ echo "salt=".$salt."/plain=".$str."
\n"; $hashed = $salt . md5(sprintf($this->getConfigInstance()->readConfig('hash_mask'), $salt, - $this->rngInstance->getExtraSalt(), - $plainPassword + $this->rngInstance->getFixedSalt(), + $str )); // And return it return $hashed; } + + /** + * Encrypt the string with fixed salt + * + * @param $str The unencrypted string + * @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()->readConfig('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))."|".base64_encode($str)."|".crc32($this->rngInstance->randomString(20)); + break; + + case 1: + $garbageString = crc32($this->rngInstance->randomString(10))."|".base64_encode($str)."|".md5($this->rngInstance->randomString(20)); + break; + + case 2: + $garbageString = crc32($this->rngInstance->randomString(10))."|".base64_encode($str)."|".sha1($this->rngInstance->randomString(20)); + break; + + case 3: + $garbageString = md5($this->rngInstance->randomString(10))."|".base64_encode($str)."|".crc32($this->rngInstance->randomString(20)); + break; + + case 4: + $garbageString = md5($this->rngInstance->randomString(10))."|".base64_encode($str)."|".md5($this->rngInstance->randomString(20)); + break; + + case 5: + $garbageString = md5($this->rngInstance->randomString(10))."|".base64_encode($str)."|".sha1($this->rngInstance->randomString(20)); + break; + + case 6: + $garbageString = sha1($this->rngInstance->randomString(10))."|".base64_encode($str)."|".crc32($this->rngInstance->randomString(20)); + break; + + case 7: + $garbageString = sha1($this->rngInstance->randomString(10))."|".base64_encode($str)."|".md5($this->rngInstance->randomString(20)); + break; + + case 8: + $garbageString = sha1($this->rngInstance->randomString(10))."|".base64_encode($str)."|".sha1($this->rngInstance->randomString(20)); + break; + } + + // Encrypt the string + $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $garbageString, MCRYPT_MODE_ECB, $iv); + + // Return the string + return $encrypted; + } + + /** + * Decrypt the string with fixed salt + * + * @param $encrypted Encrypted string + * @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()->readConfig('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("|", $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"); + + // Return the string + return $str; + } } // [EOF]