]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/Crypto.php
Move random Digits to Crypto class
[friendica.git] / src / Util / Crypto.php
index 2b1ce0f02be5b77f78f3d1844c34ddd7fd010427..785860c182887d0104bafa66e741ab9e36860372 100644 (file)
@@ -476,4 +476,30 @@ class Crypto
 
                return self::decryptAES256CBC(base64url_decode($data['data']), $k, $i);
        }
+
+
+       /**
+        * Creates cryptographic secure random digits
+        *
+        * @param string $digits The count of digits
+        * @return int The random Digits
+        */
+       public static function randomDigits($digits)
+       {
+               $rn = '';
+
+               if (!function_exists('random_int')) {
+                       // using rand() function for PHP 5.x compatibility
+                       for ($i = 0; $i < $digits; $i++) {
+                               $rn .= rand(0, 9);
+                       }
+               } else {
+                       // generating cryptographically secure pseudo-random integers
+                       for ($i = 0; $i < $digits; $i++) {
+                               $rn .= random_int(0, 9);
+                       }
+               }
+
+               return $rn;
+       }
 }