]> git.mxchange.org Git - core.git/blob - framework/main/classes/streams/crypto/mcrypt/class_McryptStream.php
1a974893c8a91f20e24202bc41eea645f72b3692
[core.git] / framework / main / classes / streams / crypto / mcrypt / class_McryptStream.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Stream\Crypto\Mcrypt;
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  * A mcrypt-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  * @todo                mcrypt will become deprecated, rewrite to OpenSSL
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 McryptStream extends BaseCryptoStream implements EncryptableStream {
35         /**
36          * Protected constructor
37          *
38          * @return      void
39          */
40         protected 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 createMcryptStream (RandomNumberGenerator $rngInstance) {
52                 // Get a new instance
53                 $streamInstance = new McryptStream();
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 ($str, $key = NULL) {
70                 // Debug message
71                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MCRYPT-STREAM: key[' . gettype($key) . ']=' . $key);
72
73                 // Init crypto module
74                 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
75                 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
76
77                 // Generate key, if none provided
78                 if (is_null($key)) {
79                         // None provided
80                         $key = $this->getRngInstance()->generateKey();
81                 } // END - if
82
83                 // Add some "payload" to the string
84                 switch ($this->getRngInstance()->randomNumber(0, 8)) {
85                         case 0:
86                                 $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
87                                 break;
88
89                         case 1:
90                                 $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
91                                 break;
92
93                         case 2:
94                                 $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
95                                 break;
96
97                         case 3:
98                                 $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
99                                 break;
100
101                         case 4:
102                                 $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
103                                 break;
104
105                         case 5:
106                                 $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
107                                 break;
108
109                         case 6:
110                                 $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
111                                 break;
112
113                         case 7:
114                                 $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
115                                 break;
116
117                         case 8:
118                                 $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
119                                 break;
120                 }
121
122                 // Encrypt the string
123                 $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $payloadString, MCRYPT_MODE_ECB, $iv);
124
125                 // Return the string
126                 return $encrypted;
127         }
128
129         /**
130          * Decrypt the string with fixed salt
131          *
132          * @param       $encrypted      Encrypted string
133          * @param       $key            Optional key, if none provided, a random key will be generated
134          * @return      $str            The unencrypted string
135          */
136         public function decryptStream ($encrypted, $key = NULL) {
137                 // Init crypto module
138                 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
139                 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
140
141                 // Shall we use a default key or custom?
142                 if (is_null($key)) {
143                         // Generate (default) key
144                         $key = $this->getRngInstance()->generateKey();
145                 } // END - if
146
147                 // Decrypt the string
148                 $payloadString = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB, $iv);
149
150                 // Get the real string out
151                 $strArray = explode(EncryptableStream::DATA_PAYLOAD_SEPARATOR, $payloadString);
152
153                 // Does the element count match?
154                 assert(count($strArray) == 3);
155
156                 // Decode the string
157                 $str = base64_decode($strArray[1]);
158
159                 // Trim trailing nulls away
160                 $str = rtrim($str, "\0");
161
162                 // Return the string
163                 return $str;
164         }
165
166         /**
167          * Streams the data and maybe does something to it
168          *
169          * @param       $data   The data (string mostly) to "stream"
170          * @return      $data   The data (string mostly) to "stream"
171          * @throws      UnsupportedOperationException   If this method is called (which is a mistake)
172          */
173         public function streamData (string $data) {
174                 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Unhandled ' . strlen($data) . ' bytes in this stream.');
175                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
176         }
177
178 }