Streamable and for encryption added, CryptoHelper (a facade) rewritten to use streams
authorRoland Häder <roland@mxchange.org>
Fri, 7 Aug 2009 22:58:45 +0000 (22:58 +0000)
committerRoland Häder <roland@mxchange.org>
Fri, 7 Aug 2009 22:58:45 +0000 (22:58 +0000)
.gitattributes
inc/classes/interfaces/streams/.htaccess [new file with mode: 0644]
inc/classes/interfaces/streams/class_Streamable.php [new file with mode: 0644]
inc/classes/interfaces/streams/crypto/.htaccess [new file with mode: 0644]
inc/classes/interfaces/streams/crypto/class_EncryptableStream.php [new file with mode: 0644]
inc/classes/main/crypto/class_CryptoHelper.php
inc/classes/main/rng/class_RandomNumberGenerator.php
inc/classes/main/streams/class_BaseStream.php
inc/classes/main/streams/crypto/class_McryptStream.php
inc/classes/main/streams/crypto/class_NullCryptoStream.php

index cf168e3..020a781 100644 (file)
@@ -204,6 +204,10 @@ inc/classes/interfaces/response/class_Responseable.php -text
 inc/classes/interfaces/result/.htaccess -text
 inc/classes/interfaces/result/class_SearchableResult.php -text
 inc/classes/interfaces/result/class_UpdateableResult.php -text
+inc/classes/interfaces/streams/.htaccess -text
+inc/classes/interfaces/streams/class_Streamable.php -text
+inc/classes/interfaces/streams/crypto/.htaccess -text
+inc/classes/interfaces/streams/crypto/class_EncryptableStream.php -text
 inc/classes/interfaces/template/.htaccess -text
 inc/classes/interfaces/template/class_CompileableTemplate.php -text
 inc/classes/interfaces/template/view/class_ViewHelper.php -text
diff --git a/inc/classes/interfaces/streams/.htaccess b/inc/classes/interfaces/streams/.htaccess
new file mode 100644 (file)
index 0000000..3a42882
--- /dev/null
@@ -0,0 +1 @@
+Deny from all
diff --git a/inc/classes/interfaces/streams/class_Streamable.php b/inc/classes/interfaces/streams/class_Streamable.php
new file mode 100644 (file)
index 0000000..9f76f71
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+/**
+ * An interface for streams
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+interface Streamable extends FrameworkInterface {
+}
+
+//
+?>
diff --git a/inc/classes/interfaces/streams/crypto/.htaccess b/inc/classes/interfaces/streams/crypto/.htaccess
new file mode 100644 (file)
index 0000000..3a42882
--- /dev/null
@@ -0,0 +1 @@
+Deny from all
diff --git a/inc/classes/interfaces/streams/crypto/class_EncryptableStream.php b/inc/classes/interfaces/streams/crypto/class_EncryptableStream.php
new file mode 100644 (file)
index 0000000..fafb299
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+/**
+ * An EncryptableStream interface
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+interface EncryptableStream extends Streamable {
+}
+
+//
+?>
index 34480b4..38a541b 100644 (file)
@@ -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
@@ -72,6 +72,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 +89,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->isPhpModuleLoaded('mcrypt')) {
+                       // Then use it
+                       $this->cryptoStreamInstance = ObjectFactory::createObjectByName('McryptStream', array($this->rngInstance()))
+               } else {
+                       // If nothing works ...
+                       $this->cryptoStreamInstance = ObjectFactory::createObjectByName('NullCryptoStream');
+               }
+       }
+
        /**
         * Initializes the hasher for different purposes.
         *
@@ -166,58 +186,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
+               $encryted = $this->cryptoStreamInstance->encryptStream($str);
 
                // Return the string
                return $encrypted;
@@ -230,31 +200,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;
index fd41643..27a3672 100644 (file)
@@ -174,6 +174,24 @@ class RandomNumberGenerator extends BaseFrameworkSystem {
        public final function getFixedSalt () {
                return $this->fixedSalt;
        }
+
+       /**
+        * Generates a key based on if we have extra (default) or fixed salt enabled
+        *
+        * @return      $key    The generated key for encrypion
+        */
+       public function generateKey () {
+               // Default is extra salt
+               $key = md5($this->getExtraSalt());
+
+               // Get key
+               if ($this->getConfigInstance()->getConfigEntry('crypt_fixed_salt') == 'Y') {
+                       $key = md5($this->getFixedSalt());
+               } // END - if
+
+               // Return it
+               return $key;
+       }
 }
 
 // [EOF]
index e226545..728e6c3 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 class BaseStream extends BaseFrameworkSystem {
+       /**
+        * Random number generator instance (RNG)
+        */
+       private $rngInstance = null;
+
        /**
         * Protected constructor
         *
@@ -36,6 +41,25 @@ class BaseStream extends BaseFrameworkSystem {
                $this->removeNumberFormaters();
                $this->removeSystemArray();
        }
+
+       /**
+        * Setter for RNG instance
+        *
+        * @param       $rngInstance    An RNG instance
+        * @return      void
+        */
+       protected final function setRngInstance (RandomNumberGenerator $rngInstance) {
+               $this->rngInstance = $rngInstance;
+       }
+
+       /**
+        * Getter for RNG instance
+        *
+        * @return      $rngInstance    An RNG instance
+        */
+       protected final function getRngInstance () {
+               return $this->rngInstance;
+       }
 }
 
 // [EOF]
index 2a0127b..23fad07 100644 (file)
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-class McryptStream extends BaseStream {
+class McryptStream extends BaseStream implements EncryptableStream {
+       /**
+        * Seperator on many places
+        */
+       private $seperator = '|';
+
        /**
         * Protected constructor
         *
@@ -35,15 +40,112 @@ class McryptStream extends BaseStream {
        /**
         * Creates an instance of this node class
         *
+        * @param       $rngInstance            An RNG instance
         * @return      $streamInstance         An instance of this node class
         */
-       public final static function createMcryptStream () {
+       public final static function createMcryptStream (RandomNumberGenerator $rngInstance) {
                // Get a new instance
                $streamInstance = new McryptStream();
 
+               // Set the RNG instance
+               $streamInstance->setRngInstance($rngInstance);
+
                // Return the instance
                return $streamInstance;
        }
+
+       /**
+        * Encrypt the string with fixed salt
+        *
+        * @param       $str            The unencrypted string
+        * @return      $encrypted      Encrypted string
+        */
+       public function encryptStream ($str) {
+               // Init crypto module
+               $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
+               $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
+
+               // Generate key
+               $key = $this->getRngInstance()->generateKey();
+
+               // Add some "garbage" to the string
+               switch ($this->getRngInstance()->randomNumber(0, 8)) {
+                       case 0:
+                               $garbageString = crc32($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . crc32($this->getRngInstance()->randomString(20));
+                               break;
+
+                       case 1:
+                               $garbageString = crc32($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . md5($this->getRngInstance()->randomString(20));
+                               break;
+
+                       case 2:
+                               $garbageString = crc32($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . sha1($this->getRngInstance()->randomString(20));
+                               break;
+
+                       case 3:
+                               $garbageString = md5($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . crc32($this->getRngInstance()->randomString(20));
+                               break;
+
+                       case 4:
+                               $garbageString = md5($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . md5($this->getRngInstance()->randomString(20));
+                               break;
+
+                       case 5:
+                               $garbageString = md5($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . sha1($this->getRngInstance()->randomString(20));
+                               break;
+
+                       case 6:
+                               $garbageString = sha1($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . crc32($this->getRngInstance()->randomString(20));
+                               break;
+
+                       case 7:
+                               $garbageString = sha1($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . md5($this->getRngInstance()->randomString(20));
+                               break;
+
+                       case 8:
+                               $garbageString = sha1($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . sha1($this->getRngInstance()->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 decryptStream ($encrypted) {
+               // Init crypto module
+               $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
+               $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
+
+               // Generate key
+               $key = $this->getRngInstance()->generateKey();
+
+               // 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");
+
+               // Return the string
+               return $str;
+       }
 }
 
 // [EOF]
index 1e905bc..864a896 100644 (file)
@@ -22,7 +22,7 @@
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-class NullCryptoStream extends BaseStream implements Streamable {
+class NullCryptoStream extends BaseStream implements EncryptableStream {
        /**
         * Protected constructor
         *