HashCreator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
c 2
b 0
f 0
dl 0
loc 35
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create_hash_id() 0 4 1
A generate_hash() 0 9 2
A generate_hash_simple() 0 3 1
1
<?php
2
3
namespace Sunnysideup\UUDI\Api;
4
5
use SilverStripe\Core\Config\Configurable;
6
use SilverStripe\Core\Injector\Injectable;
7
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
40
    {
41
        //todo - is this guessable? and does this matter? Is this a security feature?
42
        return md5(sprintf('%s:%s', $class, $id));
43
    }
44
}
45