Resource   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 22
c 2
b 1
f 0
dl 0
loc 68
rs 10
wmc 11

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getData() 0 3 1
A getIv() 0 3 1
A setKey() 0 3 1
A setData() 0 4 1
A getKey() 0 3 1
A getDKey() 0 3 1
A setLength() 0 3 1
A getLength() 0 3 1
A setIv() 0 3 1
A __construct() 0 6 1
A setDKey() 0 3 1
1
<?php
2
3
namespace Erykai\Cryption;
4
5
use Exception;
6
7
class Resource
8
{
9
    use TraitCryption;
10
11
    private int $length;
12
    private string $iv;
13
    private string $key;
14
    private string $dKey;
15
    private string $data;
16
17
    public function __construct()
18
    {
19
        $this->setLength();
20
        $this->setIv();
21
        $this->setDKey();
22
        $this->setKey();
23
    }
24
25
    private function getLength(): int
26
    {
27
        return $this->length;
28
    }
29
30
    private function setLength(): void
31
    {
32
        $this->length = openssl_cipher_iv_length(CRYPTION_CIPHERING);
33
    }
34
35
    private function getIv(): string
36
    {
37
        return $this->iv;
38
    }
39
40
    private function setIv(): void
41
    {
42
        $this->iv = random_bytes($this->getLength());
43
    }
44
45
    private function getKey(): string
46
    {
47
        return $this->key;
48
    }
49
50
    private function setKey(): void
51
    {
52
        $this->key = CRYPTION_KEY . $this->getDKey();
53
    }
54
55
56
    private function getDKey(): string
57
    {
58
        return $this->dKey;
59
    }
60
61
    private function setDKey(): void
62
    {
63
        $this->dKey = openssl_digest(php_uname(), 'MD5', TRUE);
64
    }
65
66
    protected function getData(): string
67
    {
68
        return $this->data;
69
    }
70
71
    protected function setData(string $string): void
72
    {
73
        $encrypted_data = openssl_encrypt($string, CRYPTION_CIPHERING, $this->getKey(), 0, $this->getIv());
74
        $this->data = base64_encode($encrypted_data . '.' . bin2hex($this->getDKey()) . '.' . bin2hex($this->getIv()));
75
    }
76
}
77