Passed
Push — main ( 467803...6a1b4e )
by Nicolaas
16:54 queued 08:31
created

HashCreator::create_hash_id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Sunnysideup\UUDI\Api;
4
5
class HashCreator
6
{
7
    private const CHARS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
8
9
    /**
10
     * Generate a random string, using a cryptographically secure
11
     * pseudorandom number generator (random_int).
12
     *
13
     * @param int $length How many characters do we want?
14
     */
15
    public static function generate_hash(int $length = 64): string
16
    {
17
        $pieces = [];
18
        $max = mb_strlen(self::CHARS, '8bit') - 1;
19
        for ($i = 0; $i < $length; ++$i) {
20
            $pieces[] = self::CHARS[random_int(0, $max)];
21
        }
22
23
        return implode('', $pieces);
24
    }
25
26
    public static function generate_hash_simple(int $length = 64): string
27
    {
28
        return bin2hex(random_bytes($length));
29
    }
30
31
    public static function create_hash_id(string $class, int $id): string
32
    {
33
        //todo - is this guessable? and does this matter? Is this a security feature?
34
        return md5(sprintf('%s:%s', $class, $id));
35
    }
36
37
}
38