Completed
Push — master ( 994d94...7ba070 )
by Mārtiņš
02:13
created

NonceIdentity::generateNewNonce()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Palladium\Entity;
4
5
class NonceIdentity extends Identity
6
{
7
8
    const HASH_ALGO = PASSWORD_BCRYPT;
9
    const HASH_COST = 12;
10
11
    const NONCE_SIZE = 16;
12
    const KEY_SIZE = 32;
13
14
    private $identifier;
15
    private $key;
16
    private $hash;
17
18
    protected $type = Identity::TYPE_NONCE;
19
20
21
    /**
22
     * @codeCoverageIgnore
23
     */
24
    public function setIdentifier($identifier)
25
    {
26
        $this->identifier = (string) $identifier;
27
    }
28
29
30
    /**
31
     * @codeCoverageIgnore
32
     */
33
    public function getIdentifier()
34
    {
35
        return $this->identifier;
36
    }
37
38
39 1
    public function generateNewNonce()
40
    {
41 1
        $this->identifier = bin2hex(random_bytes(self::NONCE_SIZE));
42 1
    }
43
44
45
    /**
46
     * @codeCoverageIgnore
47
     */
48
    public function getFingerprint(): string
49
    {
50
        return hash('sha384', $this->identifier);
51
    }
52
53
54
    /**
55
     * Sets a new key and resets the hash.
56
     */
57 1
    public function generateNewKey()
58
    {
59 1
        $this->key = bin2hex(random_bytes(self::KEY_SIZE));
60 1
        $this->hash = $this->makeHash($this->key);
61 1
    }
62
63
64
    /**
65
     * @param string $key
66
     */
67 3
    private function makeHash($key): string
68
    {
69 3
        return password_hash($key, self::HASH_ALGO, ['cost' => self::HASH_COST]);
70
    }
71
72
73
    /**
74
     * @param string $key
75
     */
76 1
    public function matchKey($key): bool
77
    {
78 1
        return password_verify($key, $this->hash);
79
    }
80
81
82
    /**
83
     * Assignes a new identification key and resets a the hash.
84
     *
85
     * @param string $key
86
     */
87 4 View Code Duplication
    public function setKey($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89 4
        $this->hash = null;
90
91 4
        if (empty($key)) {
92 2
            $this->key = null;
93 2
            return;
94
        }
95
96 2
        $this->key = (string) $key;
97 2
        $this->hash = $this->makeHash($key);
98 2
    }
99
100
101
    /**
102
     * @codeCoverageIgnore
103
     */
104
    public function getKey()
105
    {
106
        return $this->key;
107
    }
108
109
110
    /**
111
     * @codeCoverageIgnore
112
     * @param string $key
0 ignored issues
show
Bug introduced by
There is no parameter named $key. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
113
     */
114
    public function setHash($hash)
115
    {
116
        $this->hash = (string) $hash;
117
    }
118
119
    /**
120
     * @codeCoverageIgnore
121
     * @return string
122
     */
123
    public function getHash()
124
    {
125
        return $this->hash;
126
    }
127
}
128