Crypt::__construct()   A
last analyzed

Complexity

Conditions 6
Paths 24

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 8
c 2
b 1
f 0
dl 0
loc 15
rs 9.2222
cc 6
nc 24
nop 1
1
<?php
2
3
namespace Mfonte\Base62x\Encryption;
4
5
use Mfonte\Base62x\Exception\CryptException;
6
use Mfonte\Base62x\Encryption\Cypher\Decrypt;
7
use Mfonte\Base62x\Encryption\Cypher\Encrypt;
8
9
class Crypt
10
{
11
    // default cipher method if none supplied. see: http://php.net/openssl_get_cipher_methods for more.
12
    protected $method = 'aes-128-ctr';
13
14
    private $key;
15
16
    private $data;
17
18
    public function __construct($options = [])
19
    {
20
        //Set default encryption key if none supplied
21
        $key = isset($options['key']) ? $options['key'] : php_uname();
22
23
        $method = isset($options['method']) ? $options['method'] : false;
24
25
        // convert ASCII keys to binary format
26
        $this->key = ctype_print($key) ? openssl_digest($key, 'SHA256', true) : $key;
27
28
        if ($method) {
29
            if (\in_array(mb_strtolower($method), openssl_get_cipher_methods(), true)) {
30
                $this->method = $method;
31
            } else {
32
                throw new CryptException("unrecognised cipher method: {$method}");
33
            }
34
        }
35
    }
36
37
    public function cipher($data)
38
    {
39
        $this->data = $data;
40
41
        return $this;
42
    }
43
44
    public function encrypt()
45
    {
46
        return Encrypt::token(
47
            $this->data,
48
            $this->method,
49
            $this->key
50
        );
51
    }
52
53
    public function decrypt()
54
    {
55
        return Decrypt::token(
56
            $this->data,
57
            $this->method,
58
            $this->key
59
        );
60
    }
61
}
62