Renamed 'seperator' to 'separator'
[core.git] / inc / classes / main / streams / crypto / class_McryptStream.php
index 732ab56c838d6e9eddd92f3640e17743b7fc78f1..bb6a89d783c0613f5ad820f4ca775a883a11199b 100644 (file)
@@ -23,9 +23,9 @@
  */
 class McryptStream extends BaseStream implements EncryptableStream {
        /**
-        * Seperator on many places
+        * Separator on many places
         */
-       private $seperator = '|';
+       const DATA_PAYLOAD_SEPARATOR = '|';
 
        /**
         * Protected constructor
@@ -58,57 +58,61 @@ class McryptStream extends BaseStream implements EncryptableStream {
         * 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 encryptStream ($str) {
+       public function encryptStream ($str, $key = NULL) {
                // 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();
+               // Generate key, if none provided
+               if (is_null($key)) {
+                       // None provided
+                       $key = $this->getRngInstance()->generateKey();
+               } // END - if
 
-               // Add some "garbage" to the string
+               // Add some "payload" 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));
+                               $payloadString = crc32($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . 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));
+                               $payloadString = crc32($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . 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));
+                               $payloadString = crc32($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . 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));
+                               $payloadString = md5($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . 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));
+                               $payloadString = md5($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . 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));
+                               $payloadString = md5($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . 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));
+                               $payloadString = sha1($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . 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));
+                               $payloadString = sha1($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . 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));
+                               $payloadString = sha1($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
                                break;
                }
 
                // Encrypt the string
-               $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $garbageString, MCRYPT_MODE_ECB, $iv);
+               $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $payloadString, MCRYPT_MODE_ECB, $iv);
 
                // Return the string
                return $encrypted;
@@ -118,21 +122,25 @@ class McryptStream extends BaseStream implements EncryptableStream {
         * Decrypt the string with fixed salt
         *
         * @param       $encrypted      Encrypted string
+        * @param       $key            Optional key, if none provided, a random key will be generated
         * @return      $str            The unencrypted string
         */
-       public function decryptStream ($encrypted) {
+       public function decryptStream ($encrypted, $key = NULL) {
                // 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();
+               // Shall we use a default key or custom?
+               if (is_null($key)) {
+                       // Generate (default) key
+                       $key = $this->getRngInstance()->generateKey();
+               } // END - if
 
                // Decrypt the string
-               $garbageString = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB, $iv);
+               $payloadString = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB, $iv);
 
                // Get the real string out
-               $strArray = explode($this->seperator, $garbageString);
+               $strArray = explode(self::DATA_PAYLOAD_SEPARATOR, $payloadString);
 
                // Does the element count match?
                assert(count($strArray) == 3);
@@ -146,6 +154,18 @@ class McryptStream extends BaseStream implements EncryptableStream {
                // Return the string
                return $str;
        }
+
+       /**
+        * Streams the data and maybe does something to it
+        *
+        * @param       $data   The data (string mostly) to "stream"
+        * @return      $data   The data (string mostly) to "stream"
+        * @throws      UnsupportedOperationException   If this method is called (which is a mistake)
+        */
+       public function streamData ($data) {
+               $this->debugOutput('Unhandled ' . strlen($data) . ' bytes in this stream.');
+               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
+       }
 }
 
 // [EOF]