Updated 'core' + renamed 'main' -> 'classes'.
[hub.git] / application / hub / classes / iterator / producer / keys / class_TestUnitKeyProducerIterator.php
1 <?php
2 /**
3  * A TestUnitKeyProducer iterator
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.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 TestUnitKeyProducerIterator extends BaseIterator implements Iterator {
25         /**
26          * Maximum different bit combinations
27          */
28         private $maxBits = 0;
29
30         /**
31          * Key length
32          */
33         private $keyLength = 0;
34
35         /**
36          * Current iteration
37          */
38         private $currentIteration = 0;
39
40         /**
41          * Protected constructor
42          *
43          * @return      void
44          */
45         protected function __construct () {
46                 // Call parent constructor
47                 parent::__construct(__CLASS__);
48
49                 // Get key length
50                 $this->keyLength = $this->getConfigInstance()->getConfigEntry('test_unit_random_secret_key_length');
51
52                 // Make sure the key length isn't getting to big (32 byte = 256 bit is really, really a lot!)
53                 assert($this->keyLength <= (8 * 32));
54
55                 // Set max bits entry
56                 $this->maxBits = pow(2, $this->keyLength);
57
58                 // Debug message
59                 self::createDebugInstance(__CLASS__)->debugOutput('ITERATOR: maxBits=' . $this->maxBits . ',keyLength=' . $this->keyLength . ' bits');
60         }
61
62         /**
63          * Creates an instance of this class
64          *
65          * @return      $iteratorInstance       An instance of a Iterator class
66          */
67         public final static function createTestUnitKeyProducerIterator () {
68                 // Get new instance
69                 $iteratorInstance = new TestUnitKeyProducerIterator();
70
71                 // Return the prepared instance
72                 return $iteratorInstance;
73         }
74
75         /**
76          * Getter for current value
77          *
78          * @return      $current        Current value in iteration
79          */
80         public function current () {
81                 // Calculate ASCII string representation of the key number
82                 $current = $this->dec2asc($this->currentIteration);
83
84                 // Prepend more zeros
85                 $current = str_pad($current, ($this->keyLength / 8), chr(0), STR_PAD_LEFT);
86
87                 // Return it
88                 return $current;
89         }
90
91         /**
92          * Getter for key from group or generic
93          *
94          * @return      $key    Current key in iteration
95          * @throws      UnsupportedOperationException   This method should not be called
96          */
97         public function key () {
98                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
99         }
100
101         /**
102          * Advances to the next entry
103          *
104          * @return      void
105          */
106         public function next () {
107                 /*
108                  * This is of course a very ineffective key generation iterator because
109                  * it will create a lot of keys that will never decode an encrypted
110                  * message. If you know a better algorithm which is freely available and
111                  * can be implemented as an itertator please contact me.
112                  */
113                 $this->currentIteration++;
114         }
115
116         /**
117          * Rewinds to the beginning of the iteration
118          *
119          * @return      void
120          * @throws      UnsupportedOperationException   This method should not be called
121          */
122         public function rewind () {
123                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
124         }
125
126         /**
127          * Checks whether the current entry is valid (not at the end of the list)
128          *
129          * @return      void
130          */
131         public function valid () {
132                 return ($this->currentIteration <= $this->maxBits);
133         }
134 }
135
136 // [EOF]
137 ?>