HashCreator::generate_hash_simple()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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