Total Complexity | 9 |
Total Lines | 50 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
1 | <?php |
||
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() |
||
59 | ); |
||
60 | } |
||
61 | } |
||
62 |