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

HashCreator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A generate_hash() 0 9 2
A generate_hash_simple() 0 3 1
A create_hash_id() 0 4 1
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