Passed
Push — master ( 2340f1...c1e1ba )
by Breno
03:15 queued 01:20
created

OpenCrypt::decrypt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
crap 1
1
<?php
2
3
namespace OpenCrypt;
4
5
class OpenCrypt
6
{
7
    private $encryptMethod = "AES-256-CBC";
8
    private $secretKey;
9
    private $secretIV;
10
11 1
    public function __construct(
12
        string $secretKey,
13
        string $secretIV
14
    ) {
15
        // hash
16 1
        $this->secretKey = hash('sha256', $secretKey);
17
        // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
18 1
        $this->secretIV = substr(hash('sha256', $secretIV), 0, 16);
19 1
    }
20
21 1
    function encrypt($value) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
22 1
        $output = openssl_encrypt(
23 1
            $value,
24 1
            $this->encryptMethod,
25 1
            $this->secretKey,
26 1
            0,
27 1
            $this->secretIV
28
        );
29 1
        return base64_encode($output);
30
    }
31
32 1
    function decrypt($value) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
33 1
        return openssl_decrypt(
34 1
            base64_decode($value),
35 1
            $this->encryptMethod,
36 1
            $this->secretKey,
37 1
            0,
38 1
            $this->secretIV
39
        );
40
    }
41
}
42