RandomGeneratorFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 17
c 4
b 0
f 0
dl 0
loc 51
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A wrapAdapter() 0 7 2
A getRandomGenerator() 0 8 1
A getHmacRandomGenerator() 0 11 1
1
<?php
2
declare(strict_types=1);
3
namespace Mdanter\Ecc\Random;
4
5
use Mdanter\Ecc\Crypto\Key\PrivateKeyInterface;
6
7
use Mdanter\Ecc\Math\MathAdapterFactory;
8
9
class RandomGeneratorFactory
10
{
11
    /**
12
     * @param bool $debug
13
     * @return DebugDecorator|RandomNumberGeneratorInterface
14
     */
15
    public static function getRandomGenerator(bool $debug = false): RandomNumberGeneratorInterface
16
    {
17
        return self::wrapAdapter(
18
            new RandomNumberGenerator(
19
                MathAdapterFactory::getAdapter($debug)
20
            ),
21
            'random_bytes',
22
            $debug
23
        );
24
    }
25
26
    /**
27
     * @param PrivateKeyInterface $privateKey
28
     * @param \GMP                $messageHash
29
     * @param string              $algorithm
30
     * @param bool                $debug
31
     * @return DebugDecorator|RandomNumberGeneratorInterface
32
     */
33
    public static function getHmacRandomGenerator(PrivateKeyInterface $privateKey, \GMP $messageHash, string $algorithm, bool $debug = false): RandomNumberGeneratorInterface
34
    {
35
        return self::wrapAdapter(
36
            new HmacRandomNumberGenerator(
37
                MathAdapterFactory::getAdapter($debug),
38
                $privateKey,
39
                $messageHash,
40
                $algorithm
41
            ),
42
            'rfc6979',
43
            $debug
44
        );
45
    }
46
47
    /**
48
     * @param RandomNumberGeneratorInterface $generator
49
     * @param string                         $name
50
     * @param bool                           $debug
51
     * @return DebugDecorator|RandomNumberGeneratorInterface
52
     */
53
    private static function wrapAdapter(RandomNumberGeneratorInterface $generator, string $name, bool $debug = false): RandomNumberGeneratorInterface
54
    {
55
        if ($debug === true) {
56
            return new DebugDecorator($generator, $name);
57
        }
58
59
        return $generator;
60
    }
61
}
62