diff --git a/composer.json b/composer.json index 4820ce2..99f383d 100644 --- a/composer.json +++ b/composer.json @@ -12,12 +12,12 @@ "autoload": { "psr-4": { - "g105b\\DRNG\\": "./src" + "g105b\\drng\\": "./src" } }, "autoload-dev": { "psr-4": { - "g105b\\DRNG\\Test\\": "./test/phpunit" + "g105b\\drng\\Test\\": "./test/phpunit" } } } \ No newline at end of file diff --git a/src/DrngException.php b/src/DrngException.php new file mode 100644 index 0000000..4d035b3 --- /dev/null +++ b/src/DrngException.php @@ -0,0 +1,6 @@ +checkSeedSize($seedBytes); + $this->seedBytes = $seedBytes; // We are using OpenSSL in AES counter method, so need to retain a counter. $this->aesCounter = 0; @@ -32,6 +34,14 @@ public function getBytes(int $size):string { ); } + /** @throws SeedSizeOutOfBoundsException */ + private function checkSeedSize(string $seed):void { + $strlen = strlen($seed); + if($strlen === 0 || $strlen % 16 !== 0) { + throw new SeedSizeOutOfBoundsException(); + } + } + /** * OpenSSL is used to generate random values, according to the * initialisation vector (IV) provided. This function returns an IV diff --git a/src/SeedSizeOutOfBoundsException.php b/src/SeedSizeOutOfBoundsException.php new file mode 100644 index 0000000..1b80a0c --- /dev/null +++ b/src/SeedSizeOutOfBoundsException.php @@ -0,0 +1,4 @@ + 0 && $i % 16 === 0) { + self::assertNull($exception); + } + else { + self::assertNotNull( + $exception, + "Exception should be thrown when byte size is not a multiple of 16" + ); + } + } + } } \ No newline at end of file