TraitCryption::encryption()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 1
eloc 3
c 3
b 1
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Erykai\Cryption;
4
5
use Exception;
6
7
trait TraitCryption
8
{
9
    protected function encryption(string $string): string
10
    {
11
        $this->setData($string);
0 ignored issues
show
Bug introduced by
It seems like setData() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

11
        $this->/** @scrutinizer ignore-call */ 
12
               setData($string);
Loading history...
12
        $base = $this->getData();
0 ignored issues
show
Bug introduced by
It seems like getData() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

12
        /** @scrutinizer ignore-call */ 
13
        $base = $this->getData();
Loading history...
13
14
        return bin2hex($base);
15
    }
16
17
    protected function decryption(string $encryption): ?string
18
    {
19
        if (!ctype_xdigit($encryption)) {
20
            return null;
21
        }
22
        if (strlen($encryption) % 2 !== 0) {
23
            return null;
24
        }
25
26
        $encryption = hex2bin($encryption);
27
28
        $encryption = base64_decode($encryption);
29
        [$encrypted_data, $decryption_key, $encryption_iv] = explode(".", $encryption);
30
31
        $encryption_iv = hex2bin($encryption_iv);
32
33
        $combined_key = CRYPTION_KEY . hex2bin($decryption_key);
34
35
        return openssl_decrypt($encrypted_data, CRYPTION_CIPHERING,
36
            $combined_key, 0, $encryption_iv);
37
    }
38
39
40
}
41