Completed
Push — master ( 82bcfc...9588c4 )
by Mārtiņš
03:33
created

OneTimeIdentity::setHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Palladium\Entity;
4
5
class OneTimeIdentity 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 $nonce;
15
    private $hash;
16
17
    protected $type = Identity::TYPE_ONETIME;
18
19
20
    public function setNonce($nonce)
21
    {
22
        $this->nonce = (string) $nonce;
23
    }
24
25
26
    public function getNonce()
27
    {
28
        return $this->nonce;
29
    }
30
31
32
    public function generateNewNonce()
33
    {
34
        $this->nonce = bin2hex(random_bytes(self::NONCE_SIZE));
35
    }
36
37
38
    public function getFingerprint()
39
    {
40
        return hash('sha384', $this->nonce);
41
    }
42
43
44
    /**
45
     * Sets a new key and resets the hash.
46
     */
47
    public function generateNewKey()
48
    {
49
        $key = bin2hex(random_bytes(self::KEY_SIZE));
50
        $this->key = $key;
0 ignored issues
show
Bug introduced by
The property key does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
51
        $this->hash = $this->makeHash($key);
52
    }
53
54
55
    private function makeHash($key)
56
    {
57
        $hash = password_hash($key, self::HASH_ALGO, ['cost' => self::HASH_COST]);
58
59
        return $hash;
60
    }
61
62
63
    public function matchKey($key)
64
    {
65
        return password_verify($key, $this->hash);
66
    }
67
68
69
    /**
70
     * Assignes a new identification key and resets a the hash.
71
     *
72
     * @param string $key
73
     */
74 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...
75
    {
76
        $this->hash = null;
77
78
        if (empty($key)) {
79
            $this->key = null;
80
            return;
81
        }
82
83
        $this->key = (string) $key;
84
        $this->hash = $this->makeHash($key);
85
    }
86
87
88
    public function getKey()
89
    {
90
        return $this->key;
91
    }
92
93
94
    public function setHash($hash)
95
    {
96
        $this->hash = (string) $hash;
97
    }
98
99
    /**
100
     * @codeCoverageIgnore
101
     * @return string
102
     */
103
    public function getHash()
104
    {
105
        return $this->hash;
106
    }
107
}
108