Page MenuHomeDevCentral

No OneTemporary

diff --git a/src/Identifiers/Random.php b/src/Identifiers/Random.php
index c04e5e0..2898c60 100644
--- a/src/Identifiers/Random.php
+++ b/src/Identifiers/Random.php
@@ -1,128 +1,138 @@
<?php
declare(strict_types=1);
namespace Keruald\OmniTools\Identifiers;
use Closure;
+use Exception;
use InvalidArgumentException;
use Keruald\OmniTools\Strings\Multibyte\StringUtilities;
class Random {
/**
* @return string 32 random hexadecimal characters
*/
public static function generateHexHash () : string {
return UUID::UUIDv4WithoutHyphens();
}
/**
* @param string $format A for letters, 1 for digits, e.g. AAA111
*
* @return string a random string based on the format e.g. ZCK530
*/
public static function generateString (string $format) : string {
$randomString = "";
$len = strlen($format);
for ($i = 0 ; $i < $len ; $i++) {
$randomString .= self::generateCharacter($format[$i]);
}
return $randomString;
}
/**
* @param string $format A for letters, 1 for digits, e.g. A
*
* @return string a random string based on the format e.g. Z
*/
public static function generateCharacter (string $format) : string {
return self::getPicker(self::normalizeFormat($format))();
}
+ /**
+ * @throws Exception if an appropriate source of randomness cannot be found.
+ */
public static function generateIdentifier (int $bytes_count) : string {
$bytes = random_bytes($bytes_count);
return StringUtilities::encodeInBase64($bytes);
}
///
/// Helper methods for pickers
///
public static function normalizeFormat (string $format) : string {
$normalizers = self::getNormalizers();
foreach ($normalizers as $normalizedFormat => $conditionClosure) {
if ($conditionClosure($format)) {
return (string)$normalizedFormat;
}
}
return $format;
}
private static function getNormalizers () : array {
/**
* <normalized format> => <method which returns true if format matches>
*/
return [
'A' => function ($format) : bool {
return ctype_upper($format);
},
'a' => function ($format) : bool {
return ctype_lower($format);
},
'1' => function ($format) : bool {
return is_numeric($format);
},
];
}
private static function getPickers () : array {
return [
'A' => function () : string {
return Random::pickLetter();
},
'a' => function () : string {
return strtolower(Random::pickLetter());
},
'1' => function () : string {
return (string)Random::pickDigit();
},
];
}
+ /**
+ * @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 {
$pickers = self::getPickers();
if (isset($pickers[$format])) {
return $pickers[$format];
}
throw new InvalidArgumentException();
}
}
diff --git a/src/Identifiers/UUID.php b/src/Identifiers/UUID.php
index 2f05ac1..d91591b 100644
--- a/src/Identifiers/UUID.php
+++ b/src/Identifiers/UUID.php
@@ -1,49 +1,52 @@
<?php
declare(strict_types=1);
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
// See http://php.net/manual/en/function.uniqid.php#94959
// https://www.ietf.org/rfc/rfc4122.txt
return sprintf(
'%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)
);
}
public static function UUIDv4WithoutHyphens () : string {
return str_replace("-", "", self::UUIDv4());
}
public static function isUUID ($string) : bool {
return (bool)preg_match(self::UUID_REGEXP, $string);
}
}

File Metadata

Mime Type
text/x-diff
Expires
Sun, Nov 24, 18:14 (1 h, 32 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2258626
Default Alt Text
(5 KB)

Event Timeline