3 * A TestUnitKeyProducer iterator
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
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.
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.
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/>.
24 class TestUnitKeyProducerIterator extends BaseIterator implements Iterator {
26 * Maximum different bit combinations
33 private $keyLength = 0;
38 private $currentIteration = 0;
41 * Protected constructor
45 protected function __construct () {
46 // Call parent constructor
47 parent::__construct(__CLASS__);
50 $this->keyLength = $this->getConfigInstance()->getConfigEntry('test_unit_random_secret_key_length');
52 // Make sure the key length isn't getting to big (32 byte = 256 bit is really a lot)
53 assert($this->keyLength <= (8 * 32));
56 $this->maxBits = pow(2, $this->keyLength);
59 $this->debugOutput('ITERATOR: maxBits=' . $this->maxBits . ',keyLength=' . $this->keyLength);
63 * Creates an instance of this class
65 * @return $iteratorInstance An instance of a Iterator class
67 public final static function createTestUnitKeyProducerIterator () {
69 $iteratorInstance = new TestUnitKeyProducerIterator();
71 // Return the prepared instance
72 return $iteratorInstance;
76 * Getter for current value
78 * @return $current Current value in iteration
80 public function current () {
81 // Calculate ASCII string representation of the key number
82 $current = $this->dec2asc($this->currentIteration);
85 $current = $this->prependStringToString($current, chr(0), ($this->keyLength / 8));
92 * Getter for key from group or generic
94 * @return $key Current key in iteration
95 * @throws UnsupportedOperationException This method should not be called
97 public function key () {
98 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
102 * Advances to the next entry
106 public function next () {
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.
113 $this->currentIteration++;
117 * Rewinds to the beginning of the iteration
120 * @throws UnsupportedOperationException This method should not be called
122 public function rewind () {
123 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
127 * Checks wether the current entry is valid (not at the end of the list)
131 public function valid () {
132 return ($this->currentIteration <= $this->maxBits);