]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/iterator/producer/keys/class_TestUnitKeyProducerIterator.php
Cruncher continued and rewritten to use states:
[hub.git] / application / hub / main / iterator / producer / keys / class_TestUnitKeyProducerIterator.php
index 189eb0ee8277189e076417567f1ff6094c7f50cb..5feea77bafc7cf6af9c09c8c9158d5e0b7e14d6d 100644 (file)
  * 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
         *
@@ -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);
        }
 }