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