<?php

use Random\Engine;
use Random\Randomizer;

final class Sha1 implements Engine {
    private string $state;

    public function __construct(string $seed)
    {
        $this->state = \sha1($seed, true);
    }

    public function generate(): string
    {
        echo __METHOD__, PHP_EOL;

        $result = $this->state;
        $this->state = \sha1($result, true);

        return $result;
    }
}

final class Sha1_2 implements Engine {
    private Iterator $gen;

    public function __construct(string $seed)
    {
        $this->gen = $this->stream($seed);
    }

    private function stream($state)
    {
        while (true) {
            $state = \sha1($state, true);
            for ($i = 0; $i < \strlen($state); $i++) {
                yield $state[$i];
            }
        }
    }

    public function generate(): string
    {
        echo __METHOD__, PHP_EOL;

        $result = "";
        for ($i = 0; $i < 8; $i++) {
            $result .= $this->gen->current();
            $this->gen->next();
        }

        return $result;
    }
}


$seed = "foo";

var_dump(\bin2hex(\sha1($seed, true))); // string(40) "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"

$g = new Sha1($seed);
$r = new Randomizer($g);

var_dump(\bin2hex($r->getBytes(20))); // string(40) "0beec7b5ea3f0fdbf3a2a51a9b0f2be2778cdde7"

$g = new Sha1_2($seed);
$r = new Randomizer($g);

var_dump(\bin2hex($r->getBytes(20))); // string(40) "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"
