Registry rewritten, exception added #2
[core.git] / inc / classes / main / streams / crypto / class_McryptStream.php
1 <?php
2 /**
3  * A mcrypt-based encryption stream
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class McryptStream extends BaseStream implements EncryptableStream {
25         /**
26          * Seperator on many places
27          */
28         private $seperator = '|';
29
30         /**
31          * Protected constructor
32          *
33          * @return      void
34          */
35         protected function __construct () {
36                 // Call parent constructor
37                 parent::__construct(__CLASS__);
38         }
39
40         /**
41          * Creates an instance of this node class
42          *
43          * @param       $rngInstance            An RNG instance
44          * @return      $streamInstance         An instance of this node class
45          */
46         public final static function createMcryptStream (RandomNumberGenerator $rngInstance) {
47                 // Get a new instance
48                 $streamInstance = new McryptStream();
49
50                 // Set the RNG instance
51                 $streamInstance->setRngInstance($rngInstance);
52
53                 // Return the instance
54                 return $streamInstance;
55         }
56
57         /**
58          * Encrypt the string with fixed salt
59          *
60          * @param       $str            The unencrypted string
61          * @return      $encrypted      Encrypted string
62          */
63         public function encryptStream ($str) {
64                 // Init crypto module
65                 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
66                 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
67
68                 // Generate key
69                 $key = $this->getRngInstance()->generateKey();
70
71                 // Add some "garbage" to the string
72                 switch ($this->getRngInstance()->randomNumber(0, 8)) {
73                         case 0:
74                                 $garbageString = crc32($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . crc32($this->getRngInstance()->randomString(20));
75                                 break;
76
77                         case 1:
78                                 $garbageString = crc32($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . md5($this->getRngInstance()->randomString(20));
79                                 break;
80
81                         case 2:
82                                 $garbageString = crc32($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . sha1($this->getRngInstance()->randomString(20));
83                                 break;
84
85                         case 3:
86                                 $garbageString = md5($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . crc32($this->getRngInstance()->randomString(20));
87                                 break;
88
89                         case 4:
90                                 $garbageString = md5($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . md5($this->getRngInstance()->randomString(20));
91                                 break;
92
93                         case 5:
94                                 $garbageString = md5($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . sha1($this->getRngInstance()->randomString(20));
95                                 break;
96
97                         case 6:
98                                 $garbageString = sha1($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . crc32($this->getRngInstance()->randomString(20));
99                                 break;
100
101                         case 7:
102                                 $garbageString = sha1($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . md5($this->getRngInstance()->randomString(20));
103                                 break;
104
105                         case 8:
106                                 $garbageString = sha1($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . sha1($this->getRngInstance()->randomString(20));
107                                 break;
108                 }
109
110                 // Encrypt the string
111                 $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $garbageString, MCRYPT_MODE_ECB, $iv);
112
113                 // Return the string
114                 return $encrypted;
115         }
116
117         /**
118          * Decrypt the string with fixed salt
119          *
120          * @param       $encrypted      Encrypted string
121          * @return      $str            The unencrypted string
122          */
123         public function decryptStream ($encrypted) {
124                 // Init crypto module
125                 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
126                 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
127
128                 // Generate key
129                 $key = $this->getRngInstance()->generateKey();
130
131                 // Decrypt the string
132                 $garbageString = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB, $iv);
133
134                 // Get the real string out
135                 $strArray = explode($this->seperator, $garbageString);
136
137                 // Does the element count match?
138                 assert(count($strArray) == 3);
139
140                 // Decode the string
141                 $str = base64_decode($strArray[1]);
142
143                 // Trim trailing nulls away
144                 $str = rtrim($str, "\0");
145
146                 // Return the string
147                 return $str;
148         }
149 }
150
151 // [EOF]
152 ?>