]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/crypto/class_CryptoHelper.php
Continued:
[core.git] / framework / main / classes / crypto / class_CryptoHelper.php
index 8ac7435e3998fe6c693280447cc79dfc951fda1d..635a6ef130e1d2278d5e0afde32da666ec5368e3 100644 (file)
@@ -3,8 +3,10 @@
 namespace Org\Mxchange\CoreFramework\Helper\Crypto;
 
 // Import framework stuff
+use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
 use Org\Mxchange\CoreFramework\Crypto\Cryptable;
-use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
+use Org\Mxchange\CoreFramework\Crypto\RandomNumber\RandomNumberGenerator;
+use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
 
 /**
@@ -49,6 +51,11 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable {
         */
        private $salt = '';
 
+       /**
+        * Instance of a RNG
+        */
+       private $rngInstance = NULL;
+
        /**
         * Protected constructor
         *
@@ -94,6 +101,25 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable {
                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.
@@ -105,13 +131,13 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable {
                // Do we have openssl/mcrypt loaded?
                if ($this->isPhpExtensionLoaded('mcrypt')) {
                        // Then use it
-                       $this->cryptoStreamInstance = ObjectFactory::createObjectByName('Org\Mxchange\CoreFramework\Stream\Crypto\McryptStream', array($this->getRngInstance()));
+                       $this->cryptoStreamInstance = ObjectFactory::createObjectByConfiguredName('crypto_mcrypt_stream_class', array($this->getRngInstance()));
                } elseif ($this->isPhpExtensionLoaded('openssl')) {
                        // Then use it
-                       $this->cryptoStreamInstance = ObjectFactory::createObjectByName('Org\Mxchange\CoreFramework\Stream\Crypto\OpenSslStream', array($this->getRngInstance()));
+                       $this->cryptoStreamInstance = ObjectFactory::createObjectByConfiguredName('crypto_openssl_stream_class', array($this->getRngInstance()));
                } else {
                        // If nothing works ...
-                       $this->cryptoStreamInstance = ObjectFactory::createObjectByName('Org\Mxchange\CoreFramework\Stream\Crypto\NullCryptoStream');
+                       $this->cryptoStreamInstance = ObjectFactory::createObjectByConfiguredName('crypto_null_stream_class');
                }
        }
 
@@ -138,7 +164,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);
@@ -155,7 +181,7 @@ 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
@@ -175,17 +201,14 @@ 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);
@@ -195,14 +218,14 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable {
                //* DEBUG: */ echo "salt=".$salt."/plain=".$str."<br />\n";
                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
                        ));
@@ -219,7 +242,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);
 
@@ -233,7 +256,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);