Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3779970
D2656.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
D2656.diff
View Options
diff --git a/omnitools/src/Identifiers/Random.php b/omnitools/src/Identifiers/Random.php
--- a/omnitools/src/Identifiers/Random.php
+++ b/omnitools/src/Identifiers/Random.php
@@ -4,6 +4,7 @@
namespace Keruald\OmniTools\Identifiers;
use Closure;
+use Exception;
use InvalidArgumentException;
use Keruald\OmniTools\Strings\Multibyte\StringUtilities;
@@ -43,6 +44,9 @@
}
+ /**
+ * @throws Exception if an appropriate source of randomness cannot be found.
+ */
public static function generateIdentifier (int $bytes_count) : string {
$bytes = random_bytes($bytes_count);
@@ -105,14 +109,20 @@
];
}
+ /**
+ * @throws Exception if an appropriate source of randomness cannot be found.
+ */
public static function pickLetter () : string {
- $asciiCode = 65 + mt_rand() % 26;
+ $asciiCode = 65 + self::pickDigit(26);
return chr($asciiCode);
}
+ /**
+ * @throws Exception if an appropriate source of randomness cannot be found.
+ */
public static function pickDigit (int $base = 10) : int {
- return mt_rand() % $base;
+ return random_int(0, $base - 1);
}
private static function getPicker (string $format) : Closure {
diff --git a/omnitools/src/Identifiers/UUID.php b/omnitools/src/Identifiers/UUID.php
--- a/omnitools/src/Identifiers/UUID.php
+++ b/omnitools/src/Identifiers/UUID.php
@@ -3,12 +3,15 @@
namespace Keruald\OmniTools\Identifiers;
+use Exception;
+
class UUID {
const UUID_REGEXP = "/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/";
/**
* @return string An RFC 4122 compliant v4 UUID
+ * @throws Exception if an appropriate source of randomness cannot be found.
*/
public static function UUIDv4 () : string {
// Code by Andrew Moore
@@ -19,22 +22,22 @@
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
- mt_rand(0, 0xffff), mt_rand(0, 0xffff),
+ random_int(0, 0xffff), random_int(0, 0xffff),
// 16 bits for "time_mid"
- mt_rand(0, 0xffff),
+ random_int(0, 0xffff),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
- mt_rand(0, 0x0fff) | 0x4000,
+ random_int(0, 0x0fff) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
- mt_rand(0, 0x3fff) | 0x8000,
+ random_int(0, 0x3fff) | 0x8000,
// 48 bits for "node"
- mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
+ random_int(0, 0xffff), random_int(0, 0xffff), random_int(0, 0xffff)
);
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Tue, Nov 26, 10:55 (19 h, 57 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2264512
Default Alt Text
D2656.diff (2 KB)
Attached To
Mode
D2656: Use cryptographically secure pseudo-random integers
Attached
Detach File
Event Timeline
Log In to Comment