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