Skip to content

Commit 4b3222d

Browse files
authored
Merge pull request #109 from geekwright/repair-random
Revert workarounds added if random_bytes is not available
2 parents 7f383f7 + 850c70c commit 4b3222d

File tree

2 files changed

+11
-57
lines changed

2 files changed

+11
-57
lines changed

src/Random.php

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* @category Xmf\Random
1818
* @package Xmf
1919
* @author Richard Griffith <richard@geekwright.com>
20-
* @copyright 2015-2018 XOOPS Project (https://xoops.org)
20+
* @copyright 2015-2023 XOOPS Project (https://xoops.org)
2121
* @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
2222
* @link https://xoops.org
2323
*/
@@ -37,29 +37,7 @@ class Random
3737
*/
3838
public static function generateOneTimeToken($hash = 'sha512', $bytes = 64)
3939
{
40-
if (function_exists('random_bytes')) {
41-
$randomData = random_bytes($bytes);
42-
} elseif (function_exists('openssl_random_pseudo_bytes')) {
43-
$crypto_strong = false;
44-
$randomData = openssl_random_pseudo_bytes($bytes, $crypto_strong);
45-
46-
if ($randomData === false) {
47-
throw new Exception("Could not generate secure random bytes.");
48-
}
49-
50-
if (!$crypto_strong) {
51-
throw new Exception("Non-cryptographically strong algorithm used for random bytes.");
52-
}
53-
} else {
54-
$randomData = md5(uniqid(mt_rand(), true));
55-
}
56-
57-
if ($randomData === null) {
58-
throw new Exception("Failed to generate random data.");
59-
}
60-
61-
$token = hash($hash, $randomData);
62-
40+
$token = hash($hash, random_bytes($bytes));
6341
return $token;
6442
}
6543

@@ -77,28 +55,7 @@ public static function generateOneTimeToken($hash = 'sha512', $bytes = 64)
7755
*/
7856
public static function generateKey($hash = 'sha512', $bytes = 128)
7957
{
80-
if (function_exists('random_bytes')) {
81-
$randomData = random_bytes($bytes);
82-
} elseif (function_exists('openssl_random_pseudo_bytes')) {
83-
$crypto_strong = false;
84-
$randomData = openssl_random_pseudo_bytes($bytes, $crypto_strong);
85-
86-
if ($randomData === false) {
87-
throw new Exception("Could not generate secure random bytes.");
88-
}
89-
90-
if (!$crypto_strong) {
91-
throw new Exception("Non-cryptographically strong algorithm used for random bytes.");
92-
}
93-
} else {
94-
$randomData = md5(uniqid(mt_rand(), true));
95-
}
96-
97-
if ($randomData === null) {
98-
throw new Exception("Failed to generate random data.");
99-
}
100-
101-
$token = hash($hash, $randomData);
58+
$token = hash($hash, random_bytes($bytes));
10259
return $token;
10360
}
10461
}

src/Ulid.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Ulid
3030
*
3131
* @return string The generated ULID.
3232
*/
33-
public static function generate(bool $upperCase = true): string
33+
public static function generate($upperCase = true)
3434
{
3535
$time = self::microtimeToUlidTime(\microtime(true));
3636
$timeChars = self::encodeTime($time);
@@ -47,7 +47,7 @@ public static function generate(bool $upperCase = true): string
4747
*
4848
* @return string
4949
*/
50-
public static function encodeTime(int $time): string
50+
public static function encodeTime($time)
5151
{
5252
$encodingCharsArray = str_split(self::ENCODING_CHARS);
5353
$timeChars = '';
@@ -59,7 +59,7 @@ public static function encodeTime(int $time): string
5959
return $timeChars;
6060
}
6161

62-
public static function encodeRandomness(): string
62+
public static function encodeRandomness()
6363
{
6464
$encodingCharsArray = str_split(self::ENCODING_CHARS);
6565
$randomBytes = \random_bytes(10); // 80 bits
@@ -86,7 +86,7 @@ public static function encodeRandomness(): string
8686
*
8787
* @return array
8888
*/
89-
public static function decode(string $ulid): array
89+
public static function decode($ulid)
9090
{
9191
if (!self::isValid($ulid)) {
9292
throw new \InvalidArgumentException('Invalid ULID string');
@@ -106,7 +106,7 @@ public static function decode(string $ulid): array
106106
*
107107
* @return int
108108
*/
109-
public static function decodeTime(string $ulid): int
109+
public static function decodeTime($ulid)
110110
{
111111
// $encodingCharsArray = str_split(self::ENCODING_CHARS);
112112

@@ -131,7 +131,7 @@ public static function decodeTime(string $ulid): int
131131
*
132132
* @return int
133133
*/
134-
public static function decodeRandomness(string $ulid): int
134+
public static function decodeRandomness($ulid)
135135
{
136136
if (26 !== strlen($ulid)) {
137137
throw new \InvalidArgumentException('Invalid ULID length'); // Changed line
@@ -158,7 +158,7 @@ public static function decodeRandomness(string $ulid): int
158158
*
159159
* @return bool
160160
*/
161-
public static function isValid(string $ulid): bool
161+
public static function isValid($ulid)
162162
{
163163
// Check the length of the ULID string before throwing an exception.
164164
if (26 !== strlen($ulid)) {
@@ -180,14 +180,11 @@ public static function isValid(string $ulid): bool
180180
*
181181
* @return int
182182
*/
183-
public static function microtimeToUlidTime(float $microtime): int
183+
public static function microtimeToUlidTime($microtime)
184184
{
185185
$timestamp = $microtime * 1000000;
186186
$unixEpoch = 946684800000000; // Microseconds since the Unix epoch.
187187

188188
return (int)($timestamp - $unixEpoch);
189189
}
190190
}
191-
192-
193-

0 commit comments

Comments
 (0)