Streamable and for encryption added, CryptoHelper (a facade) rewritten to use streams
[core.git] / inc / classes / main / streams / crypto / class_McryptStream.php
index 2a0127b3ec52f682f27c6e958266d5ab488bc465..23fad07b893189428fe9021859ad1ba7bc7e8887 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]