Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert workarounds added if random_bytes is not available #109

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 3 additions & 46 deletions src/Random.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @category Xmf\Random
* @package Xmf
* @author Richard Griffith <[email protected]>
* @copyright 2015-2018 XOOPS Project (https://xoops.org)
* @copyright 2015-2023 XOOPS Project (https://xoops.org)
* @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
* @link https://xoops.org
*/
Expand All @@ -37,29 +37,7 @@ class Random
*/
public static function generateOneTimeToken($hash = 'sha512', $bytes = 64)
{
if (function_exists('random_bytes')) {
$randomData = random_bytes($bytes);
} elseif (function_exists('openssl_random_pseudo_bytes')) {
$crypto_strong = false;
$randomData = openssl_random_pseudo_bytes($bytes, $crypto_strong);

if ($randomData === false) {
throw new Exception("Could not generate secure random bytes.");
}

if (!$crypto_strong) {
throw new Exception("Non-cryptographically strong algorithm used for random bytes.");
}
} else {
$randomData = md5(uniqid(mt_rand(), true));
}

if ($randomData === null) {
throw new Exception("Failed to generate random data.");
}

$token = hash($hash, $randomData);

$token = hash($hash, random_bytes($bytes));
return $token;
}

Expand All @@ -77,28 +55,7 @@ public static function generateOneTimeToken($hash = 'sha512', $bytes = 64)
*/
public static function generateKey($hash = 'sha512', $bytes = 128)
{
if (function_exists('random_bytes')) {
$randomData = random_bytes($bytes);
} elseif (function_exists('openssl_random_pseudo_bytes')) {
$crypto_strong = false;
$randomData = openssl_random_pseudo_bytes($bytes, $crypto_strong);

if ($randomData === false) {
throw new Exception("Could not generate secure random bytes.");
}

if (!$crypto_strong) {
throw new Exception("Non-cryptographically strong algorithm used for random bytes.");
}
} else {
$randomData = md5(uniqid(mt_rand(), true));
}

if ($randomData === null) {
throw new Exception("Failed to generate random data.");
}

$token = hash($hash, $randomData);
$token = hash($hash, random_bytes($bytes));
return $token;
}
}
19 changes: 8 additions & 11 deletions src/Ulid.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Ulid
*
* @return string The generated ULID.
*/
public static function generate(bool $upperCase = true): string
public static function generate($upperCase = true)
{
$time = self::microtimeToUlidTime(\microtime(true));
$timeChars = self::encodeTime($time);
Expand All @@ -47,7 +47,7 @@ public static function generate(bool $upperCase = true): string
*
* @return string
*/
public static function encodeTime(int $time): string
public static function encodeTime($time)
{
$encodingCharsArray = str_split(self::ENCODING_CHARS);
$timeChars = '';
Expand All @@ -59,7 +59,7 @@ public static function encodeTime(int $time): string
return $timeChars;
}

public static function encodeRandomness(): string
public static function encodeRandomness()
{
$encodingCharsArray = str_split(self::ENCODING_CHARS);
$randomBytes = \random_bytes(10); // 80 bits
Expand All @@ -86,7 +86,7 @@ public static function encodeRandomness(): string
*
* @return array
*/
public static function decode(string $ulid): array
public static function decode($ulid)
{
if (!self::isValid($ulid)) {
throw new \InvalidArgumentException('Invalid ULID string');
Expand All @@ -106,7 +106,7 @@ public static function decode(string $ulid): array
*
* @return int
*/
public static function decodeTime(string $ulid): int
public static function decodeTime($ulid)
{
// $encodingCharsArray = str_split(self::ENCODING_CHARS);

Expand All @@ -131,7 +131,7 @@ public static function decodeTime(string $ulid): int
*
* @return int
*/
public static function decodeRandomness(string $ulid): int
public static function decodeRandomness($ulid)
{
if (26 !== strlen($ulid)) {
throw new \InvalidArgumentException('Invalid ULID length'); // Changed line
Expand All @@ -158,7 +158,7 @@ public static function decodeRandomness(string $ulid): int
*
* @return bool
*/
public static function isValid(string $ulid): bool
public static function isValid($ulid)
{
// Check the length of the ULID string before throwing an exception.
if (26 !== strlen($ulid)) {
Expand All @@ -180,14 +180,11 @@ public static function isValid(string $ulid): bool
*
* @return int
*/
public static function microtimeToUlidTime(float $microtime): int
public static function microtimeToUlidTime($microtime)
{
$timestamp = $microtime * 1000000;
$unixEpoch = 946684800000000; // Microseconds since the Unix epoch.

return (int)($timestamp - $unixEpoch);
}
}



Loading