]> git.mxchange.org Git - core.git/blobdiff - inc/classes/main/crypto/class_CryptoHelper.php
Some cleanups, more usage of ObjectFactory:
[core.git] / inc / classes / main / crypto / class_CryptoHelper.php
index 2335f5dc5a534855c556de2034d315fe8b2c1206..0c626bba48025d4427bee36666cb393be3af1845 100644 (file)
@@ -4,7 +4,7 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -29,17 +29,12 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable {
        /**
         * An instance of this own clas
         */
-       private static $selfInstance = null;
-
-       /**
-        * Instance of the random number generator
-        */
-       private $rngInstance = null;
+       private static $selfInstance = NULL;
 
        /**
         * Instance of the crypto stream
         */
-       private $cryptoStreamInstance = null;
+       private $cryptoStreamInstance = NULL;
 
        /**
         * Salt for hashing operations
@@ -54,10 +49,6 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable {
        protected function __construct () {
                // Call parent constructor
                parent::__construct(__CLASS__);
-
-               // Clean up a little
-               $this->removeNumberFormaters();
-               $this->removeSystemArray();
        }
 
        /**
@@ -65,7 +56,7 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable {
         *
         * @return      $cryptoInstance         An instance of this crypto helper class
         */
-       public final static function createCryptoHelper () {
+       public static final function createCryptoHelper () {
                // Get a new instance
                $cryptoInstance = new CryptoHelper();
 
@@ -84,7 +75,7 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable {
         *
         * @return      $selfInstance   An instance of this crypto helper class
         */
-       public final static function getInstance () {
+       public static final function getInstance () {
                // Is no instance there?
                if (is_null(self::$selfInstance)) {
                        // Then get a new one
@@ -105,7 +96,7 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable {
                // Do we have mcrypt loaded?
                if ($this->isPhpExtensionLoaded('mcrypt')) {
                        // Then use it
-                       $this->cryptoStreamInstance = ObjectFactory::createObjectByName('McryptStream', array($this->rngInstance()));
+                       $this->cryptoStreamInstance = ObjectFactory::createObjectByName('McryptStream', array($this->getRngInstance()));
                } else {
                        // If nothing works ...
                        $this->cryptoStreamInstance = ObjectFactory::createObjectByName('NullCryptoStream');
@@ -119,7 +110,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();
@@ -132,7 +123,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');
@@ -171,7 +162,7 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable {
                //* DEBUG: */ echo "salt=".$salt."/plain=".$str."<br />\n";
                $hashed = $salt . md5(sprintf($this->getConfigInstance()->getConfigEntry('hash_mask'),
                        $salt,
-                       $this->rngInstance->getFixedSalt(),
+                       $this->getRngInstance()->getFixedSalt(),
                        $str
                ));
 
@@ -183,11 +174,12 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable {
         * Encrypt the string with fixed salt
         *
         * @param       $str            The unencrypted string
+        * @param       $key            Optional key, if none provided, a random key will be generated
         * @return      $encrypted      Encrypted string
         */
-       public function encryptString ($str) {
+       public function encryptString ($str, $key = NULL) {
                // Encrypt the string through the stream
-               $encryted = $this->cryptoStreamInstance->encryptStream($str);
+               $encrypted = $this->cryptoStreamInstance->encryptStream($str, $key);
 
                // Return the string
                return $encrypted;