]> git.mxchange.org Git - core.git/blob - framework/main/classes/streams/crypto/openssl/class_OpenSslStream.php
Continued:
[core.git] / framework / main / classes / streams / crypto / openssl / class_OpenSslStream.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Stream\Crypto\OpenSsl;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Crypto\RandomNumber\RandomNumberGenerator;
7 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
8 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
9 use Org\Mxchange\CoreFramework\Stream\Crypto\BaseCryptoStream;
10 use Org\Mxchange\CoreFramework\Stream\Crypto\EncryptableStream;
11
12 /**
13  * An OpenSSL-based encryption stream
14  *
15  * @author              Roland Haeder <webmaster@shipsimu.org>
16  * @version             0.0.0
17  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
18  * @license             GNU GPL 3.0 or any newer version
19  * @link                http://www.shipsimu.org
20  *
21  * This program is free software: you can redistribute it and/or modify
22  * it under the terms of the GNU General Public License as published by
23  * the Free Software Foundation, either version 3 of the License, or
24  * (at your option) any later version.
25  *
26  * This program is distributed in the hope that it will be useful,
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29  * GNU General Public License for more details.
30  *
31  * You should have received a copy of the GNU General Public License
32  * along with this program. If not, see <http://www.gnu.org/licenses/>.
33  */
34 class OpenSslStream extends BaseCryptoStream implements EncryptableStream {
35         /**
36          * Protected constructor
37          *
38          * @return      void
39          */
40         private function __construct () {
41                 // Call parent constructor
42                 parent::__construct(__CLASS__);
43         }
44
45         /**
46          * Creates an instance of this node class
47          *
48          * @param       $rngInstance            An RNG instance
49          * @return      $streamInstance         An instance of this node class
50          */
51         public static final function createOpenSslStream (RandomNumberGenerator $rngInstance) {
52                 // Get a new instance
53                 $streamInstance = new OpenSslStream();
54
55                 // Set the RNG instance
56                 $streamInstance->setRngInstance($rngInstance);
57
58                 // Return the instance
59                 return $streamInstance;
60         }
61
62         /**
63          * Encrypt the string with fixed salt
64          *
65          * @param       $str            The unencrypted string
66          * @param       $key            Optional key, if none provided, a random key will be generated
67          * @return      $encrypted      Encrypted string
68          */
69         public function encryptStream (string $str, string $key = NULL) {
70                 // @TODO unfinished
71                 return $str;
72
73                 // Debug message
74                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('OPENSSL-STREAM: key[' . gettype($key) . ']=' . $key);
75
76                 // Init crypto module
77                 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
78                 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
79
80                 // Generate key, if none provided
81                 if (is_null($key)) {
82                         // None provided
83                         $key = $this->getRngInstance()->generateKey();
84                 }
85
86                 // Add some "payload" to the string
87                 switch ($this->getRngInstance()->randomNumber(0, 8)) {
88                         case 0:
89                                 $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
90                                 break;
91
92                         case 1:
93                                 $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
94                                 break;
95
96                         case 2:
97                                 $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
98                                 break;
99
100                         case 3:
101                                 $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
102                                 break;
103
104                         case 4:
105                                 $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
106                                 break;
107
108                         case 5:
109                                 $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
110                                 break;
111
112                         case 6:
113                                 $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
114                                 break;
115
116                         case 7:
117                                 $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
118                                 break;
119
120                         case 8:
121                                 $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
122                                 break;
123                 }
124
125                 // Encrypt the string
126                 $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $payloadString, MCRYPT_MODE_ECB, $iv);
127
128                 // Return the string
129                 return $encrypted;
130         }
131
132         /**
133          * Decrypt the string with fixed salt
134          *
135          * @param       $encrypted      Encrypted string
136          * @param       $key            Optional key, if none provided, a random key will be generated
137          * @return      $str            The unencrypted string
138          */
139         public function decryptStream (string $encrypted, string $key = NULL) {
140                 // @TODO unfinished
141                 return $encrypted;
142
143                 // Init crypto module
144                 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
145                 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
146
147                 // Shall we use a default key or custom?
148                 if (is_null($key)) {
149                         // Generate (default) key
150                         $key = $this->getRngInstance()->generateKey();
151                 }
152
153                 // Decrypt the string
154                 $payloadString = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB, $iv);
155
156                 // Get the real string out
157                 $strArray = explode(EncryptableStream::DATA_PAYLOAD_SEPARATOR, $payloadString);
158
159                 // Does the element count match?
160                 assert(count($strArray) == 3);
161
162                 // Decode the string
163                 $str = base64_decode($strArray[1]);
164
165                 // Trim trailing nulls away
166                 $str = rtrim($str, "\0");
167
168                 // Return the string
169                 return $str;
170         }
171
172         /**
173          * Streams the data and maybe does something to it
174          *
175          * @param       $data   The data (string mostly) to "stream"
176          * @return      $data   The data (string mostly) to "stream"
177          * @throws      UnsupportedOperationException   If this method is called (which is a mistake)
178          */
179         public function streamData (string $data) {
180                 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Unhandled ' . strlen($data) . ' bytes in this stream.');
181                 throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);
182         }
183
184 }