]> git.mxchange.org Git - core.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Sat, 1 Feb 2025 22:19:38 +0000 (23:19 +0100)
committerRoland Häder <roland@mxchange.org>
Sat, 1 Feb 2025 22:19:38 +0000 (23:19 +0100)
- added more scalar type-hints

framework/main/classes/scrypt/class_Scrypt.php

index fdd661ed546590c0917935880d3540ebf5f8d4b6..1b609ef22312941baf87156052ae04a2fbca3dfe 100644 (file)
@@ -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);
     }
+
 }