Secret::setAlgorithm()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 3
eloc 8
c 2
b 0
f 1
nc 4
nop 2
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 3
rs 10
1
<?php
2
3
namespace TQ\Shamir;
4
5
use OutOfBoundsException;
6
use TQ\Shamir\Algorithm\Algorithm;
7
use TQ\Shamir\Algorithm\RandomGeneratorAware;
8
use TQ\Shamir\Algorithm\Shamir;
9
use TQ\Shamir\Random\Generator;
10
use TQ\Shamir\Random\PhpGenerator;
11
12
/**
13
 * Class Secret
14
 *
15
 * This is a simple static facade to Shamir's shared secret algorithm
16
 *
17
 * @package TQ\Shamir
18
 */
19
class Secret
20
{
21
    /**
22
     * The random generator
23
     *
24
     * @var Generator|null
25
     */
26
    protected static $randomGenerator;
27
28
    /**
29
     * The algorithm
30
     *
31
     * @var Algorithm|null
32
     */
33
    protected static $algorithm;
34
35
    /**
36
     * Overrides the random generator to use
37
     *
38
     * @param  Generator|null  $randomGenerator  The random generator
39
     * @param  boolean         $returnOld        True to return the old random generator
40
     *
41
     * @return  Generator|null The old random generator if $returnOld is true
42
     */
43 63
    public static function setRandomGenerator(Generator $randomGenerator = null, $returnOld = true)
44
    {
45 63
        if ($returnOld) {
46 63
            $oldRandomGenerator = self::getRandomGenerator();
47
        } else {
48
            $oldRandomGenerator = null;
49
        }
50 63
        self::$randomGenerator = $randomGenerator;
51
52 63
        $algorithm = self::$algorithm;
53 63
        if ($algorithm instanceof RandomGeneratorAware) {
54 7
            $algorithm->setRandomGenerator(self::getRandomGenerator());
55
        }
56
57 63
        return $oldRandomGenerator;
58
    }
59
60
    /**
61
     * Returns the random generator
62
     *
63
     * @return  Generator
64
     */
65 63
    public static function getRandomGenerator()
66
    {
67 63
        if (!self::$randomGenerator) {
68 63
            self::$randomGenerator = new PhpGenerator();
69
        }
70
71 63
        return self::$randomGenerator;
72
    }
73
74
    /**
75
     * Returns the algorithm
76
     */
77 63
    public static function getAlgorithm(): ?Algorithm
78
    {
79 63
        if (!self::$algorithm) {
80 63
            self::setAlgorithm(new Shamir(), false);
81
        }
82
83 63
        return self::$algorithm;
84
    }
85
86
    /**
87
     * Overrides the algorithm to use
88
     *
89
     * @param  Algorithm|null  $algorithm
90
     * @param  boolean         $returnOld  True to return the old algorithm
91
     *
92
     * @return  Algorithm|null The old algorithm if $returnOld is true
93
     */
94 63
    public static function setAlgorithm(Algorithm $algorithm = null, $returnOld = true): ?Algorithm
95
    {
96 63
        if ($returnOld) {
97 63
            $oldAlgorithm = self::getAlgorithm();
98
        } else {
99 63
            $oldAlgorithm = null;
100
        }
101
102 63
        if ($algorithm instanceof RandomGeneratorAware) {
103 63
            $algorithm->setRandomGenerator(self::getRandomGenerator());
104
        }
105 63
        self::$algorithm = $algorithm;
106
107 63
        return $oldAlgorithm;
108
    }
109
110
    /**
111
     * Generate shared secrets
112
     *
113
     * @param  string  $secret     Secret
114
     * @param  int     $shares     Number of parts to share
115
     * @param  int     $threshold  Minimum number of shares required for decryption
116
     *
117
     * @return  array              Secret shares
118
     * @throws  OutOfBoundsException
119
     */
120 5
    public static function share(string $secret, int $shares, int $threshold = 2): array
121
    {
122 5
        return self::getAlgorithm()->share($secret, $shares, $threshold);
123
    }
124
125
    /**
126
     * Recovers the secret from the given shared keys
127
     */
128 4
    public static function recover(array $keys): string
129
    {
130 4
        return self::getAlgorithm()->recover($keys);
131
    }
132
}
133