X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=application%2Fhub%2Fmain%2Fiterator%2Fproducer%2Fkeys%2Fclass_TestUnitKeyProducerIterator.php;h=5feea77bafc7cf6af9c09c8c9158d5e0b7e14d6d;hb=eedbbb80ac6f49f2d43e3ae9a18008a25cfdc16e;hp=189eb0ee8277189e076417567f1ff6094c7f50cb;hpb=ecfe3c8077fa94964dfab3423e189db81b721a74;p=hub.git diff --git a/application/hub/main/iterator/producer/keys/class_TestUnitKeyProducerIterator.php b/application/hub/main/iterator/producer/keys/class_TestUnitKeyProducerIterator.php index 189eb0ee8..5feea77ba 100644 --- a/application/hub/main/iterator/producer/keys/class_TestUnitKeyProducerIterator.php +++ b/application/hub/main/iterator/producer/keys/class_TestUnitKeyProducerIterator.php @@ -22,6 +22,21 @@ * along with this program. If not, see . */ 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 * @@ -30,6 +45,18 @@ class TestUnitKeyProducerIterator extends BaseIterator implements Iterator { 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); } /** @@ -46,15 +73,16 @@ class TestUnitKeyProducerIterator extends BaseIterator implements Iterator { } /** - * 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; @@ -76,7 +104,13 @@ class TestUnitKeyProducerIterator extends BaseIterator implements Iterator { * @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++; } /** @@ -95,7 +129,7 @@ class TestUnitKeyProducerIterator extends BaseIterator implements Iterator { * @return void */ public function valid () { - $this->partialStub('Please implement this method.'); + return ($this->currentIteration <= $this->maxBits); } }