From: Roland Häder Date: Sat, 1 Feb 2025 22:19:38 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=6f9ae3684fdaef58edf77d7a20ec199bbef9fb39;p=core.git Continued: - added more scalar type-hints --- diff --git a/framework/main/classes/scrypt/class_Scrypt.php b/framework/main/classes/scrypt/class_Scrypt.php index fdd661ed..1b609ef2 100644 --- a/framework/main/classes/scrypt/class_Scrypt.php +++ b/framework/main/classes/scrypt/class_Scrypt.php @@ -7,6 +7,9 @@ use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap; use Org\Mxchange\CoreFramework\Feature\FrameworkFeature; use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem; +// Import SPL stuff +use \InvalidArgumentException; + /** * This file contains a 'core-d' version of the example helper classes for the * php-scrypt extension. It has been renamed from scrypt.php to this name so the @@ -19,7 +22,7 @@ use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem; * As with all cryptographic code; it is recommended that you use a tried and * tested library which uses this library; rather than rolling your own. * - * PHP version 5 + * PHP version 8 * * @category Security * @package Scrypt @@ -44,7 +47,6 @@ use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem; */ abstract class Scrypt extends BaseFrameworkSystem { - /** * * @var int The key length @@ -58,7 +60,8 @@ abstract class Scrypt extends BaseFrameworkSystem * * @return int */ - protected static function strlen ($str) { + protected static function strlen (string $str): int + { static $isShadowed = null; if ($isShadowed === null) { @@ -80,7 +83,7 @@ abstract class Scrypt extends BaseFrameworkSystem * * @return string The salt */ - public static function generateScryptSalt ($length = 8) + public static function generateScryptSalt (int $length = 8): str { $buffer = ''; $buffer_valid = false; @@ -135,23 +138,23 @@ abstract class Scrypt extends BaseFrameworkSystem * * @return string The hashed password */ - public static function hashScrypt ($password, $salt = false, $N = 16384, $r = 8, $p = 1) + public static function hashScrypt (string $password, string $salt = '', int $N = 16384, int $r = 8, int $p = 1): str { if (!FrameworkFeature::isFeatureAvailable('hubcoin_reward')) { // Feature has been disabled - throw new \InvalidArgumentException('Feature "scrypt" disabled.'); + throw new InvalidArgumentException('Feature "scrypt" disabled.'); } if ($N == 0 || ($N & ($N - 1)) != 0) { - throw new \InvalidArgumentException('N must be > 0 and a power of 2'); + throw new InvalidArgumentException('N must be > 0 and a power of 2'); } if ($N > PHP_INT_MAX / 128 / $r) { - throw new \InvalidArgumentException('Parameter N is too large'); + throw new InvalidArgumentException('Parameter N is too large'); } if ($r > PHP_INT_MAX / 128 / $p) { - throw new \InvalidArgumentException('Parameter r is too large'); + throw new InvalidArgumentException('Parameter r is too large'); } if ($salt === false) { @@ -174,7 +177,7 @@ abstract class Scrypt extends BaseFrameworkSystem * * @return boolean If the clear text matches */ - public static function checkScrypt ($password, $hash) + public static function checkScrypt (string $password, string $hash): bool { // Is there actually a hash? if (!$hash) { @@ -183,7 +186,7 @@ abstract class Scrypt extends BaseFrameworkSystem if (!FrameworkFeature::isFeatureAvailable('hubcoin_reward')) { // Feature has been disabled - throw new \InvalidArgumentException('Feature "scrypt" disabled.'); + throw new InvalidArgumentException('Feature "scrypt" disabled.'); } list ($N, $r, $p, $salt, $hash) = explode('$', $hash); @@ -223,7 +226,7 @@ abstract class Scrypt extends BaseFrameworkSystem * * @return boolean If the two strings match. */ - public static function compareScryptHashes ($expected, $actual) + public static function compareScryptHashes (string $expected, string $actual): bool { $expected = (string) $expected; $actual = (string) $actual; @@ -239,4 +242,5 @@ abstract class Scrypt extends BaseFrameworkSystem return ($result === 0); } + }