X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=framework%2Fmain%2Fclasses%2Fcrypto%2Fclass_CryptoHelper.php;h=e913a91bc05d2f89ee79418d449043a52d6f6a70;hb=refs%2Fheads%2Fmaster;hp=153f4d4f781daacb89c535f6cf5b73e8db031ff2;hpb=78a010fef84895720e796842208f01dfb619c332;p=core.git diff --git a/framework/main/classes/crypto/class_CryptoHelper.php b/framework/main/classes/crypto/class_CryptoHelper.php index 153f4d4f..e913a91b 100644 --- a/framework/main/classes/crypto/class_CryptoHelper.php +++ b/framework/main/classes/crypto/class_CryptoHelper.php @@ -1,17 +1,20 @@ * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.shipsimu.org * @@ -48,12 +51,17 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable { */ private $salt = ''; + /** + * Instance of a RNG + */ + private $rngInstance = NULL; + /** * Protected constructor * * @return void */ - protected function __construct () { + private function __construct () { // Call parent constructor parent::__construct(__CLASS__); } @@ -65,15 +73,19 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable { */ public static final function createCryptoHelper () { // Get a new instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CRYPTO-HELPER: CALLED!'); $cryptoInstance = new CryptoHelper(); // Initialize the hasher + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CRYPTO-HELPER: Invoking cryptoInstance->initHasher() ...'); $cryptoInstance->initHasher(); // Attach a crypto stream + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CRYPTO-HELPER: Invoking cryptoInstance->attachCryptoStream() ...'); $cryptoInstance->attachCryptoStream(); // Return the instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CRYPTO-HELPER: cryptoInstance=%s - EXIT!', $cryptoInstance->__toString())); return $cryptoInstance; } @@ -84,15 +96,36 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable { */ public static final function getSelfInstance () { // Is no instance there? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CRYPTO-HELPER: self::selfInstance[]=%s - CALLED!', gettype(self::$selfInstance))); if (is_null(self::$selfInstance)) { // Then get a new one self::$selfInstance = self::createCryptoHelper(); - } // END - if + } // Return the instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CRYPTO-HELPER: self::selfInstance=%s - EXIT!', self::$selfInstance->__toString())); return self::$selfInstance; } + /** + * Setter for RNG instance + * + * @param $rngInstance An instance of a random number generator (RNG) + * @return void + */ + protected final function setRngInstance (RandomNumberGenerator $rngInstance) { + $this->rngInstance = $rngInstance; + } + + /** + * Getter for RNG instance + * + * @return $rngInstance An instance of a random number generator (RNG) + */ + public final function getRngInstance () { + return $this->rngInstance; + } + /** * Attaches a crypto stream to this crypto helper by detecting loaded * modules. @@ -100,14 +133,21 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable { * @return void */ protected function attachCryptoStream () { - // Do we have mcrypt loaded? - if ($this->isPhpExtensionLoaded('mcrypt')) { + // @TODO Maybe rewrite this with DirectoryIterator, similar to Compressor thing? + // Do we have openssl loaded? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CRYPTO-HELPER: CALLED!'); + if ($this->isPhpExtensionLoaded('openssl')) { // Then use it - $this->cryptoStreamInstance = ObjectFactory::createObjectByName('McryptStream', array($this->getRngInstance())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('CRYPTO-HELPER: Attaching openssl crypto stream ...'); + $this->cryptoStreamInstance = ObjectFactory::createObjectByConfiguredName('crypto_openssl_stream_class', [$this->getRngInstance()]); } else { // If nothing works ... - $this->cryptoStreamInstance = ObjectFactory::createObjectByName('NullCryptoStream'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('CRYPTO-HELPER: Attaching NULL crypto stream ...'); + $this->cryptoStreamInstance = ObjectFactory::createObjectByConfiguredName('crypto_null_stream_class'); } + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CRYPTO-HELPER: EXIT!'); } /** @@ -133,7 +173,7 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable { $randomString = $this->getRngInstance()->randomString() . $this->createUuid(); // Get config entry for salt length - $length = $this->getConfigInstance()->getConfigEntry('salt_length'); + $length = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('salt_length'); // Keep only defined number of characters $this->salt = substr(sha1($randomString), -$length, $length); @@ -150,10 +190,10 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable { $uuid = ''; // Is the UUID extension loaded and enabled? (see pecl) - if ($this->getConfigInstance()->getConfigEntry('extension_uuid_loaded') === TRUE) { + if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('extension_uuid_loaded') === true) { // Then add it as well $uuid = uuid_create(); - } // END - if + } // Return it return $uuid; @@ -170,34 +210,31 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable { * @param $withFixed Whether to include a fixed salt (not recommended in p2p applications) * @return $hashed The hashed and salted string */ - public function hashString ($str, $oldHash = '', $withFixed = TRUE) { - // Cast the string - $str = (string) $str; - + public function hashString (string $str, string $oldHash = '', bool $withFixed = true) { // Default is the default salt ;-) $salt = $this->salt; // Is the old password set? if (!empty($oldHash)) { // Use the salt from hash, first get length - $length = $this->getConfigInstance()->getConfigEntry('salt_length'); + $length = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('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"; - if ($withFixed === TRUE) { + if ($withFixed === true) { // Use additional fixed salt - $hashed = $salt . md5(sprintf($this->getConfigInstance()->getConfigEntry('hash_extra_mask'), + $hashed = $salt . md5(sprintf(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('hash_extra_mask'), $salt, $this->getRngInstance()->getFixedSalt(), $str )); } else { // Use salt+string to hash - $hashed = $salt . md5(sprintf($this->getConfigInstance()->getConfigEntry('hash_normal_mask'), + $hashed = $salt . md5(sprintf(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('hash_normal_mask'), $salt, $str )); @@ -214,7 +251,7 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable { * @param $key Optional key, if none provided, a random key will be generated * @return $encrypted Encrypted string */ - public function encryptString ($str, $key = NULL) { + public function encryptString (string $str, string $key = NULL) { // Encrypt the string through the stream $encrypted = $this->cryptoStreamInstance->encryptStream($str, $key); @@ -228,7 +265,7 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable { * @param $encrypted Encrypted string * @return $str The unencrypted string */ - public function decryptString ($encrypted) { + public function decryptString (string $encrypted) { // Encrypt the string through the stream $str = $this->cryptoStreamInstance->decryptStream($encrypted);