]> git.mxchange.org Git - core.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Sat, 7 Nov 2020 18:08:35 +0000 (19:08 +0100)
committerRoland Häder <roland@mxchange.org>
Sat, 7 Nov 2020 18:10:36 +0000 (19:10 +0100)
- proper packages/namespaces and common BaseCryptoStream class
- also $rngInstance must be here, too

Signed-off-by: Roland Häder <roland@mxchange.org>
framework/config-global.php
framework/main/classes/streams/crypto/class_BaseCryptoStream.php [new file with mode: 0644]
framework/main/classes/streams/crypto/class_McryptStream.php [deleted file]
framework/main/classes/streams/crypto/class_NullCryptoStream.php [deleted file]
framework/main/classes/streams/crypto/class_OpenSslStream.php [deleted file]
framework/main/classes/streams/crypto/mcrypt/class_McryptStream.php [new file with mode: 0644]
framework/main/classes/streams/crypto/null/class_NullCryptoStream.php [new file with mode: 0644]
framework/main/classes/streams/crypto/openssl/class_OpenSslStream.php [new file with mode: 0644]

index 1a3819e9aceae9d64d3fdadefe1d39e62727567a..8ba651e39e37a8da5d633459035c7118d69c156c 100644 (file)
@@ -467,10 +467,10 @@ $cfg->setConfigEntry('thousands_separator', '.');
 $cfg->setConfigEntry('decimals_separator', ',');
 
 // CFG: CRYPTO-MCRYPT-STREAM-CLASS
-$cfg->setConfigEntry('crypto_mcrypt_stream_class', 'Org\Mxchange\CoreFramework\Stream\Crypto\McryptStream');
+$cfg->setConfigEntry('crypto_mcrypt_stream_class', 'Org\Mxchange\CoreFramework\Stream\Crypto\Mcrypt\McryptStream');
 
 // CFG: CRYPTO-OPENSSL-STREAM-CLASS
-$cfg->setConfigEntry('crypto_openssl_stream_class', 'Org\Mxchange\CoreFramework\Stream\Crypto\OpenSslStream');
+$cfg->setConfigEntry('crypto_openssl_stream_class', 'Org\Mxchange\CoreFramework\Stream\Crypto\OpenSsl\OpenSslStream');
 
 // CFG: CRYPTO-NULL-STREAM-CLASS
-$cfg->setConfigEntry('crypto_null_stream_class', 'Org\Mxchange\CoreFramework\Stream\Crypto\NullCryptoStream');
+$cfg->setConfigEntry('crypto_null_stream_class', 'Org\Mxchange\CoreFramework\Stream\Crypto\Null\NullCryptoStream');
diff --git a/framework/main/classes/streams/crypto/class_BaseCryptoStream.php b/framework/main/classes/streams/crypto/class_BaseCryptoStream.php
new file mode 100644 (file)
index 0000000..b8ffcac
--- /dev/null
@@ -0,0 +1,67 @@
+<?php
+// Own namespace
+namespace Org\Mxchange\CoreFramework\Stream\Crypto;
+
+// Import framework stuff
+use Org\Mxchange\CoreFramework\Crypto\RandomNumber\RandomNumberGenerator;
+use Org\Mxchange\CoreFramework\Stream\BaseStream;
+
+/**
+ * A general crypto stream class
+ *
+ * @author             Roland Haeder <webmaster@shipsimu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.shipsimu.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/>.
+ */
+abstract class BaseCryptoStream extends BaseStream {
+       /**
+        * Instance of a RNG
+        */
+       private $rngInstance = NULL;
+
+       /**
+        * Protected constructor
+        *
+        * @param       $className      Name of the class
+        * @return      void
+        */
+       protected function __construct (string $className) {
+               // Call parent constructor
+               parent::__construct($className);
+       }
+
+       /**
+        * 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)
+        */
+       protected final function getRngInstance () {
+               return $this->rngInstance;
+       }
+
+}
diff --git a/framework/main/classes/streams/crypto/class_McryptStream.php b/framework/main/classes/streams/crypto/class_McryptStream.php
deleted file mode 100644 (file)
index a704498..0000000
+++ /dev/null
@@ -1,177 +0,0 @@
-<?php
-// Own namespace
-namespace Org\Mxchange\CoreFramework\Stream\Crypto;
-
-// Import framework stuff
-use Org\Mxchange\CoreFramework\Crypto\RandomNumber\RandomNumberGenerator;
-use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
-use Org\Mxchange\CoreFramework\Stream\BaseStream;
-
-/**
- * A mcrypt-based encryption stream
- *
- * @author             Roland Haeder <webmaster@shipsimu.org>
- * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
- * @license            GNU GPL 3.0 or any newer version
- * @link               http://www.shipsimu.org
- * @todo               mcrypt will become deprecated, rewrite to OpenSSL
- *
- * 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/>.
- */
-class McryptStream extends BaseStream implements EncryptableStream {
-       /**
-        * Protected constructor
-        *
-        * @return      void
-        */
-       protected function __construct () {
-               // Call parent constructor
-               parent::__construct(__CLASS__);
-       }
-
-       /**
-        * Creates an instance of this node class
-        *
-        * @param       $rngInstance            An RNG instance
-        * @return      $streamInstance         An instance of this node class
-        */
-       public static final 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
-        * @param       $key            Optional key, if none provided, a random key will be generated
-        * @return      $encrypted      Encrypted string
-        */
-       public function encryptStream ($str, $key = NULL) {
-               // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MCRYPT-STREAM: key[' . gettype($key) . ']=' . $key);
-
-               // 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, if none provided
-               if (is_null($key)) {
-                       // None provided
-                       $key = $this->getRngInstance()->generateKey();
-               } // END - if
-
-               // Add some "payload" to the string
-               switch ($this->getRngInstance()->randomNumber(0, 8)) {
-                       case 0:
-                               $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
-                               break;
-
-                       case 1:
-                               $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
-                               break;
-
-                       case 2:
-                               $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
-                               break;
-
-                       case 3:
-                               $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
-                               break;
-
-                       case 4:
-                               $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
-                               break;
-
-                       case 5:
-                               $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
-                               break;
-
-                       case 6:
-                               $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
-                               break;
-
-                       case 7:
-                               $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
-                               break;
-
-                       case 8:
-                               $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
-                               break;
-               }
-
-               // Encrypt the string
-               $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $payloadString, MCRYPT_MODE_ECB, $iv);
-
-               // Return the string
-               return $encrypted;
-       }
-
-       /**
-        * 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, $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);
-
-               // Shall we use a default key or custom?
-               if (is_null($key)) {
-                       // Generate (default) key
-                       $key = $this->getRngInstance()->generateKey();
-               } // END - if
-
-               // Decrypt the string
-               $payloadString = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB, $iv);
-
-               // Get the real string out
-               $strArray = explode(EncryptableStream::DATA_PAYLOAD_SEPARATOR, $payloadString);
-
-               // 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;
-       }
-
-       /**
-        * 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) {
-               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Unhandled ' . strlen($data) . ' bytes in this stream.');
-               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
-       }
-
-}
diff --git a/framework/main/classes/streams/crypto/class_NullCryptoStream.php b/framework/main/classes/streams/crypto/class_NullCryptoStream.php
deleted file mode 100644 (file)
index 1c1fe4a..0000000
+++ /dev/null
@@ -1,97 +0,0 @@
-<?php
-// Own namespace
-namespace Org\Mxchange\CoreFramework\Stream\Crypto;
-
-// Import framework stuff
-use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
-use Org\Mxchange\CoreFramework\Stream\BaseStream;
-
-/**
- * A null-encryption stream does not encrypt anything but can be used if e.e.
- * mcrypt is not installed.
- *
- * @author             Roland Haeder <webmaster@shipsimu.org>
- * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009  Developer Team
- * @license            GNU GPL 3.0 or any newer version
- * @link               http://www.shipsimu.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/>.
- */
-class NullCryptoStream extends BaseStream implements EncryptableStream {
-       /**
-        * Protected constructor
-        *
-        * @return      void
-        */
-       protected function __construct () {
-               // Call parent constructor
-               parent::__construct(__CLASS__);
-       }
-
-       /**
-        * Creates an instance of this node class
-        *
-        * @return      $streamInstance         An instance of this node class
-        */
-       public static final function createNullCryptoStream () {
-               // Get a new instance
-               $streamInstance = new NullCryptoStream();
-
-               // Return the instance
-               return $streamInstance;
-       }
-
-       /**
-        * 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, $key = NULL) {
-               // Just handle it over
-               $encrypted = (string) $str;
-
-               // Return it
-               return $encrypted;
-       }
-
-       /**
-        * Decrypt the string with fixed salt
-        *
-        * @param       $encrypted      Encrypted string
-        * @return      $str            The unencrypted string
-        */
-       public function decryptStream ($encrypted) {
-               // Just handle it over
-               $str = (string) $encrypted;
-
-               // Return it
-               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) {
-               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Unhandled ' . strlen($data) . ' bytes in this stream.');
-               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
-       }
-
-}
diff --git a/framework/main/classes/streams/crypto/class_OpenSslStream.php b/framework/main/classes/streams/crypto/class_OpenSslStream.php
deleted file mode 100644 (file)
index c0efc04..0000000
+++ /dev/null
@@ -1,182 +0,0 @@
-<?php
-// Own namespace
-namespace Org\Mxchange\CoreFramework\Stream\Crypto;
-
-// Import framework stuff
-use Org\Mxchange\CoreFramework\Crypto\RandomNumber\RandomNumberGenerator;
-use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
-use Org\Mxchange\CoreFramework\Stream\BaseStream;
-
-/**
- * An OpenSSL-based encryption stream
- *
- * @author             Roland Haeder <webmaster@shipsimu.org>
- * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
- * @license            GNU GPL 3.0 or any newer version
- * @link               http://www.shipsimu.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/>.
- */
-class OpenSslStream extends BaseStream implements EncryptableStream {
-       /**
-        * Protected constructor
-        *
-        * @return      void
-        */
-       protected function __construct () {
-               // Call parent constructor
-               parent::__construct(__CLASS__);
-       }
-
-       /**
-        * Creates an instance of this node class
-        *
-        * @param       $rngInstance            An RNG instance
-        * @return      $streamInstance         An instance of this node class
-        */
-       public static final function createOpenSslStream (RandomNumberGenerator $rngInstance) {
-               // Get a new instance
-               $streamInstance = new OpenSslStream();
-
-               // Set the RNG instance
-               $streamInstance->setRngInstance($rngInstance);
-
-               // Return the instance
-               return $streamInstance;
-       }
-
-       /**
-        * 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, $key = NULL) {
-               // @TODO unfinished
-               return $str;
-
-               // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('OPENSSL-STREAM: key[' . gettype($key) . ']=' . $key);
-
-               // 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, if none provided
-               if (is_null($key)) {
-                       // None provided
-                       $key = $this->getRngInstance()->generateKey();
-               } // END - if
-
-               // Add some "payload" to the string
-               switch ($this->getRngInstance()->randomNumber(0, 8)) {
-                       case 0:
-                               $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
-                               break;
-
-                       case 1:
-                               $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
-                               break;
-
-                       case 2:
-                               $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
-                               break;
-
-                       case 3:
-                               $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
-                               break;
-
-                       case 4:
-                               $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
-                               break;
-
-                       case 5:
-                               $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
-                               break;
-
-                       case 6:
-                               $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
-                               break;
-
-                       case 7:
-                               $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
-                               break;
-
-                       case 8:
-                               $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
-                               break;
-               }
-
-               // Encrypt the string
-               $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $payloadString, MCRYPT_MODE_ECB, $iv);
-
-               // Return the string
-               return $encrypted;
-       }
-
-       /**
-        * 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, $key = NULL) {
-               // @TODO unfinished
-               return $encrypted;
-
-               // Init crypto module
-               $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
-               $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
-
-               // Shall we use a default key or custom?
-               if (is_null($key)) {
-                       // Generate (default) key
-                       $key = $this->getRngInstance()->generateKey();
-               } // END - if
-
-               // Decrypt the string
-               $payloadString = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB, $iv);
-
-               // Get the real string out
-               $strArray = explode(EncryptableStream::DATA_PAYLOAD_SEPARATOR, $payloadString);
-
-               // 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;
-       }
-
-       /**
-        * 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) {
-               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Unhandled ' . strlen($data) . ' bytes in this stream.');
-               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
-       }
-
-}
diff --git a/framework/main/classes/streams/crypto/mcrypt/class_McryptStream.php b/framework/main/classes/streams/crypto/mcrypt/class_McryptStream.php
new file mode 100644 (file)
index 0000000..265b181
--- /dev/null
@@ -0,0 +1,178 @@
+<?php
+// Own namespace
+namespace Org\Mxchange\CoreFramework\Stream\Crypto\Mcrypt;
+
+// Import framework stuff
+use Org\Mxchange\CoreFramework\Crypto\RandomNumber\RandomNumberGenerator;
+use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
+use Org\Mxchange\CoreFramework\Stream\Crypto\BaseCryptoStream;
+use Org\Mxchange\CoreFramework\Stream\Crypto\EncryptableStream;
+
+/**
+ * A mcrypt-based encryption stream
+ *
+ * @author             Roland Haeder <webmaster@shipsimu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.shipsimu.org
+ * @todo               mcrypt will become deprecated, rewrite to OpenSSL
+ *
+ * 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/>.
+ */
+class McryptStream extends BaseCryptoStream implements EncryptableStream {
+       /**
+        * Protected constructor
+        *
+        * @return      void
+        */
+       protected function __construct () {
+               // Call parent constructor
+               parent::__construct(__CLASS__);
+       }
+
+       /**
+        * Creates an instance of this node class
+        *
+        * @param       $rngInstance            An RNG instance
+        * @return      $streamInstance         An instance of this node class
+        */
+       public static final 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
+        * @param       $key            Optional key, if none provided, a random key will be generated
+        * @return      $encrypted      Encrypted string
+        */
+       public function encryptStream ($str, $key = NULL) {
+               // Debug message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MCRYPT-STREAM: key[' . gettype($key) . ']=' . $key);
+
+               // 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, if none provided
+               if (is_null($key)) {
+                       // None provided
+                       $key = $this->getRngInstance()->generateKey();
+               } // END - if
+
+               // Add some "payload" to the string
+               switch ($this->getRngInstance()->randomNumber(0, 8)) {
+                       case 0:
+                               $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
+                               break;
+
+                       case 1:
+                               $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
+                               break;
+
+                       case 2:
+                               $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
+                               break;
+
+                       case 3:
+                               $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
+                               break;
+
+                       case 4:
+                               $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
+                               break;
+
+                       case 5:
+                               $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
+                               break;
+
+                       case 6:
+                               $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
+                               break;
+
+                       case 7:
+                               $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
+                               break;
+
+                       case 8:
+                               $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
+                               break;
+               }
+
+               // Encrypt the string
+               $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $payloadString, MCRYPT_MODE_ECB, $iv);
+
+               // Return the string
+               return $encrypted;
+       }
+
+       /**
+        * 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, $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);
+
+               // Shall we use a default key or custom?
+               if (is_null($key)) {
+                       // Generate (default) key
+                       $key = $this->getRngInstance()->generateKey();
+               } // END - if
+
+               // Decrypt the string
+               $payloadString = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB, $iv);
+
+               // Get the real string out
+               $strArray = explode(EncryptableStream::DATA_PAYLOAD_SEPARATOR, $payloadString);
+
+               // 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;
+       }
+
+       /**
+        * 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) {
+               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Unhandled ' . strlen($data) . ' bytes in this stream.');
+               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
+       }
+
+}
diff --git a/framework/main/classes/streams/crypto/null/class_NullCryptoStream.php b/framework/main/classes/streams/crypto/null/class_NullCryptoStream.php
new file mode 100644 (file)
index 0000000..6564497
--- /dev/null
@@ -0,0 +1,98 @@
+<?php
+// Own namespace
+namespace Org\Mxchange\CoreFramework\Stream\Crypto\Null;
+
+// Import framework stuff
+use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
+use Org\Mxchange\CoreFramework\Stream\Crypto\BaseCryptoStream;
+use Org\Mxchange\CoreFramework\Stream\Crypto\EncryptableStream;
+
+/**
+ * A null-encryption stream does not encrypt anything but can be used if e.e.
+ * mcrypt is not installed.
+ *
+ * @author             Roland Haeder <webmaster@shipsimu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009  Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.shipsimu.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/>.
+ */
+class NullCryptoStream extends BaseCryptoStream implements EncryptableStream {
+       /**
+        * Protected constructor
+        *
+        * @return      void
+        */
+       protected function __construct () {
+               // Call parent constructor
+               parent::__construct(__CLASS__);
+       }
+
+       /**
+        * Creates an instance of this node class
+        *
+        * @return      $streamInstance         An instance of this node class
+        */
+       public static final function createNullCryptoStream () {
+               // Get a new instance
+               $streamInstance = new NullCryptoStream();
+
+               // Return the instance
+               return $streamInstance;
+       }
+
+       /**
+        * 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, $key = NULL) {
+               // Just handle it over
+               $encrypted = (string) $str;
+
+               // Return it
+               return $encrypted;
+       }
+
+       /**
+        * Decrypt the string with fixed salt
+        *
+        * @param       $encrypted      Encrypted string
+        * @return      $str            The unencrypted string
+        */
+       public function decryptStream ($encrypted) {
+               // Just handle it over
+               $str = (string) $encrypted;
+
+               // Return it
+               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) {
+               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Unhandled ' . strlen($data) . ' bytes in this stream.');
+               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
+       }
+
+}
diff --git a/framework/main/classes/streams/crypto/openssl/class_OpenSslStream.php b/framework/main/classes/streams/crypto/openssl/class_OpenSslStream.php
new file mode 100644 (file)
index 0000000..b81bd1a
--- /dev/null
@@ -0,0 +1,183 @@
+<?php
+// Own namespace
+namespace Org\Mxchange\CoreFramework\Stream\Crypto\OpenSsl;
+
+// Import framework stuff
+use Org\Mxchange\CoreFramework\Crypto\RandomNumber\RandomNumberGenerator;
+use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
+use Org\Mxchange\CoreFramework\Stream\Crypto\BaseCryptoStream;
+use Org\Mxchange\CoreFramework\Stream\Crypto\EncryptableStream;
+
+/**
+ * An OpenSSL-based encryption stream
+ *
+ * @author             Roland Haeder <webmaster@shipsimu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.shipsimu.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/>.
+ */
+class OpenSslStream extends BaseCryptoStream implements EncryptableStream {
+       /**
+        * Protected constructor
+        *
+        * @return      void
+        */
+       protected function __construct () {
+               // Call parent constructor
+               parent::__construct(__CLASS__);
+       }
+
+       /**
+        * Creates an instance of this node class
+        *
+        * @param       $rngInstance            An RNG instance
+        * @return      $streamInstance         An instance of this node class
+        */
+       public static final function createOpenSslStream (RandomNumberGenerator $rngInstance) {
+               // Get a new instance
+               $streamInstance = new OpenSslStream();
+
+               // Set the RNG instance
+               $streamInstance->setRngInstance($rngInstance);
+
+               // Return the instance
+               return $streamInstance;
+       }
+
+       /**
+        * 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, $key = NULL) {
+               // @TODO unfinished
+               return $str;
+
+               // Debug message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('OPENSSL-STREAM: key[' . gettype($key) . ']=' . $key);
+
+               // 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, if none provided
+               if (is_null($key)) {
+                       // None provided
+                       $key = $this->getRngInstance()->generateKey();
+               } // END - if
+
+               // Add some "payload" to the string
+               switch ($this->getRngInstance()->randomNumber(0, 8)) {
+                       case 0:
+                               $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
+                               break;
+
+                       case 1:
+                               $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
+                               break;
+
+                       case 2:
+                               $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
+                               break;
+
+                       case 3:
+                               $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
+                               break;
+
+                       case 4:
+                               $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
+                               break;
+
+                       case 5:
+                               $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
+                               break;
+
+                       case 6:
+                               $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
+                               break;
+
+                       case 7:
+                               $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
+                               break;
+
+                       case 8:
+                               $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
+                               break;
+               }
+
+               // Encrypt the string
+               $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $payloadString, MCRYPT_MODE_ECB, $iv);
+
+               // Return the string
+               return $encrypted;
+       }
+
+       /**
+        * 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, $key = NULL) {
+               // @TODO unfinished
+               return $encrypted;
+
+               // Init crypto module
+               $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
+               $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
+
+               // Shall we use a default key or custom?
+               if (is_null($key)) {
+                       // Generate (default) key
+                       $key = $this->getRngInstance()->generateKey();
+               } // END - if
+
+               // Decrypt the string
+               $payloadString = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB, $iv);
+
+               // Get the real string out
+               $strArray = explode(EncryptableStream::DATA_PAYLOAD_SEPARATOR, $payloadString);
+
+               // 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;
+       }
+
+       /**
+        * 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) {
+               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Unhandled ' . strlen($data) . ' bytes in this stream.');
+               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
+       }
+
+}