Passed
Push — main ( cd254c...1dff49 )
by Alex
01:07
created

Resource::getKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Erykai\Cryption;
4
5
use Exception;
6
7
/**
8
 *
9
 */
10
class Resource
11
{
12
    use TraitCryption;
13
14
    /**
15
     * @var int
16
     */
17
    private int $length;
18
    /**
19
     * @var string
20
     */
21
    private string $iv;
22
    /**
23
     * @var string
24
     */
25
    private string $key;
26
    /**
27
     * @var string
28
     */
29
    private string $dKey;
30
    /**
31
     * @var string
32
     */
33
    private string $data;
34
35
    /**
36
     * @throws Exception
37
     */
38
    public function __construct()
39
    {
40
        $this->setLength();
41
        $this->setIv();
42
        $this->setKey();
43
        $this->setDKey();
44
    }
45
46
    /**
47
     * @return int
48
     */
49
    protected function getLength(): int
50
    {
51
        return $this->length;
52
    }
53
54
    /**
55
     * setLength
56
     */
57
    private function setLength(): void
58
    {
59
        $this->length = openssl_cipher_iv_length(CIPHERING);
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    protected function getIv(): string
66
    {
67
        return $this->iv;
68
    }
69
70
    /**
71
     * @throws Exception
72
     */
73
    private function setIv(): void
74
    {
75
        $this->iv = substr(base64_encode(random_bytes($this->getLength())), 0, $this->getLength());
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    protected function getKey(): string
82
    {
83
        return $this->key;
84
    }
85
86
87
    /**
88
     * setKey
89
     */
90
    private function setKey(): void
91
    {
92
        $this->key = base64_encode(DECRYPT_KEY) . $this->getDKey();
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    protected function getDKey(): string
99
    {
100
        return $this->dKey;
101
    }
102
103
    /**
104
     * setDKey
105
     */
106
    private function setDKey(): void
107
    {
108
        $this->dKey = openssl_digest(php_uname(), 'MD5', TRUE);
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    protected function getData(): string
115
    {
116
        return $this->data;
117
    }
118
119
    /**
120
     * @param string $string
121
     */
122
    protected function setData(string $string): void
123
    {
124
        $this->data = base64_encode(
125
            openssl_encrypt($string, CIPHERING, $this->getKey(), iv: $this->getKey()) .'.'.
126
            $this->getDKey() .'.'.
127
            $this->getIv()
128
        );
129
    }
130
131
}