From 1f8812c939cb7ac1964aa09f24dffc2b52b7bb76 Mon Sep 17 00:00:00 2001 From: Hide_D Date: Thu, 8 Jun 2023 23:23:05 +0900 Subject: [PATCH] fix: MAX_RNG_SUPPORT_BIT warning --- src/sammo/LiteHashDRBG.php | 20 ++++++++++---------- src/sammo/NoRNG.php | 5 +++-- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/sammo/LiteHashDRBG.php b/src/sammo/LiteHashDRBG.php index 6a205d3c..01495c68 100644 --- a/src/sammo/LiteHashDRBG.php +++ b/src/sammo/LiteHashDRBG.php @@ -2,12 +2,6 @@ namespace sammo; -//NOTE: JavaScript 버전과 일치 -const MAX_RNG_SUPPORT_BIT = 53; -if (PHP_INT_SIZE * 8 < MAX_RNG_SUPPORT_BIT) { - throw new \RangeException('PHP not support '.MAX_RNG_SUPPORT_BIT.' bit integer'); -} - /** * Reseed를 하지 않는 단순한 형태의 sha512 drbg * float: bit를 강제로 채움 @@ -16,7 +10,9 @@ if (PHP_INT_SIZE * 8 < MAX_RNG_SUPPORT_BIT) { class LiteHashDRBG implements RNG { - const MAX_INT = (1 << MAX_RNG_SUPPORT_BIT) - 1; + //NOTE: JavaScript 버전과 일치 + const MAX_RNG_SUPPORT_BIT = 53; + const MAX_INT = (1 << self::MAX_RNG_SUPPORT_BIT) - 1; const BUFFER_BYTE_SIZE = 512 / 8; //SHA512 protected string $buffer; @@ -182,7 +178,7 @@ class LiteHashDRBG implements RNG public function nextInt(?int $max = null): int { if ($max === null || $max === self::MAX_INT) { - $buffer = $this->nextBits(MAX_RNG_SUPPORT_BIT) . "\x00"; + $buffer = $this->nextBits(self::MAX_RNG_SUPPORT_BIT) . "\x00"; return self::parseU64($buffer); } @@ -207,9 +203,9 @@ class LiteHashDRBG implements RNG public function nextFloat1(): float { - $max = 1 << MAX_RNG_SUPPORT_BIT; + $max = 1 << self::MAX_RNG_SUPPORT_BIT; while (true) { - $buffer = $this->nextBits(MAX_RNG_SUPPORT_BIT + 1) . "\x00"; + $buffer = $this->nextBits(self::MAX_RNG_SUPPORT_BIT + 1) . "\x00"; $nInt = self::parseU64($buffer); if ($nInt <= $max) { break; @@ -223,3 +219,7 @@ class LiteHashDRBG implements RNG return new LiteHashDRBG($seed, $idx); } } + +if (PHP_INT_SIZE * 8 < LiteHashDRBG::MAX_RNG_SUPPORT_BIT) { + throw new \RangeException('PHP not support '.LiteHashDRBG::MAX_RNG_SUPPORT_BIT.' bit integer'); +} \ No newline at end of file diff --git a/src/sammo/NoRNG.php b/src/sammo/NoRNG.php index 122fef26..3d70477b 100644 --- a/src/sammo/NoRNG.php +++ b/src/sammo/NoRNG.php @@ -2,14 +2,15 @@ namespace sammo; -const MAX_RNG_SUPPORT_BIT = 53; + /** * 내부에 랜덤 값을 호출하지 않을 것이 확실할 때 RNG 대용으로 사용 * 어떤 함수를 호출하든 에러 발생 */ class NoRNG implements RNG { - const MAX_INT = (1 << MAX_RNG_SUPPORT_BIT) - 1; + const MAX_RNG_SUPPORT_BIT = 53; + const MAX_INT = (1 << self::MAX_RNG_SUPPORT_BIT) - 1; public function __construct() { }