Total Complexity | 4 |
Total Lines | 35 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
8 | class HashCreator |
||
9 | { |
||
10 | use Configurable; |
||
11 | use Injectable; |
||
12 | |||
13 | private static $always_require_uuid = false; |
||
14 | |||
15 | private const CHARS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
||
16 | |||
17 | /** |
||
18 | * Generate a random string, using a cryptographically secure |
||
19 | * pseudorandom number generator (random_int). |
||
20 | * |
||
21 | * @param int $length How many characters do we want? |
||
22 | */ |
||
23 | public static function generate_hash(int $length = 64): string |
||
24 | { |
||
25 | $pieces = []; |
||
26 | $max = mb_strlen(self::CHARS, '8bit') - 1; |
||
27 | for ($i = 0; $i < $length; ++$i) { |
||
28 | $pieces[] = self::CHARS[random_int(0, $max)]; |
||
29 | } |
||
30 | |||
31 | return implode('', $pieces); |
||
32 | } |
||
33 | |||
34 | public static function generate_hash_simple(int $length = 64): string |
||
35 | { |
||
36 | return bin2hex(random_bytes($length)); |
||
37 | } |
||
38 | |||
39 | public static function create_hash_id(string $class, int $id): string |
||
43 | } |
||
44 | } |
||
45 |