* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class TestUnitKeyProducerIterator extends BaseIterator implements Iterator {
+ /**
+ * Maximum different bit combinations
+ */
+ private $maxBits = 0;
+
+ /**
+ * Key length
+ */
+ private $keyLength = 0;
+
+ /**
+ * Current iteration
+ */
+ private $currentIteration = 0;
+
/**
* Protected constructor
*
protected function __construct () {
// Call parent constructor
parent::__construct(__CLASS__);
+
+ // Get key length
+ $this->keyLength = $this->getConfigInstance()->getConfigEntry('test_unit_random_secret_key_length');
+
+ // Make sure the key length isn't getting to big (32 byte = 256 bit is really a lot)
+ assert($this->keyLength <= (8 * 32));
+
+ // Set max bits entry
+ $this->maxBits = pow(2, $this->keyLength);
+
+ // Debug message
+ $this->debugOutput('ITERATOR: maxBits=' . $this->maxBits . ',keyLength=' . $this->keyLength);
}
/**
}
/**
- * Getter for current value from group or generic
+ * Getter for current value
*
* @return $current Current value in iteration
*/
public function current () {
- // Default is null
- $current = null;
+ // Calculate ASCII string representation of the key number
+ $current = $this->dec2asc($this->currentIteration);
- $this->partialStub('Please implement this method.');
+ // Prepend more zeros
+ $current = $this->prependStringToString($current, chr(0), ($this->keyLength / 8));
// Return it
return $current;
* @return void
*/
public function next () {
- $this->partialStub('Please implement this method.');
+ /*
+ * This is of course a very ineffective key generation iterator because
+ * it will create a lot of keys that will never decode an encrypted
+ * message. If you know a better algorithm which is freely available and
+ * can be implemented as an itertator please contact me.
+ */
+ $this->currentIteration++;
}
/**
* @return void
*/
public function valid () {
- $this->partialStub('Please implement this method.');
+ return ($this->currentIteration <= $this->maxBits);
}
}