Proyectos de Subversion LeadersLinked - Services

Rev

Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
218 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Library;
6
 
7
class AesEncryption {
8
        protected $key;
9
        protected $data;
10
        protected $method;
11
        /**
12
         * Available OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING
13
         *
14
 
15
         */
16
        protected $options = 0;
17
 
18
        function __construct($data = null, $key = null, $blockSize = null, $mode = 'CBC') {
19
            $this->setData($data);
20
            $this->setKey($key);
21
            $this->setMethode($blockSize, $mode);
22
        }
23
 
24
        public function setData($data) {
25
            $this->data = $data;
26
        }
27
 
28
        public function setKey($key) {
29
            $this->key = $key;
30
        }
31
        /**
32
         * CBC 128 192 256
33
         CBC-HMAC-SHA1 128 256
34
         CBC-HMAC-SHA256 128 256
35
         CFB 128 192 256
36
         CFB1 128 192 256
37
         CFB8 128 192 256
38
         CTR 128 192 256
39
         ECB 128 192 256
40
         OFB 128 192 256
41
         XTS 128 256
42
 
43
         */
44
        public function setMethode($blockSize, $mode = 'CBC') {
45
            if($blockSize==192 && in_array('', array('CBC-HMAC-SHA1','CBC-HMAC-SHA256','XTS'))){
46
                $this->method=null;
47
                throw new \Exception('Invlid block size and mode combination!');
48
            }
49
            $this->method = 'AES-' . $blockSize . '-' . $mode;
50
        }
51
        /**
52
         *
53
         * @return boolean
54
         */
55
        public function validateParams() {
56
            if ($this->data != null &&
57
                $this->method != null ) {
58
                    return true;
59
                } else {
60
                    return FALSE;
61
                }
62
        }
63
        //it must be the same when you encrypt and decrypt
64
        protected function getIV() {
65
            return '1234567890123456';
66
            //return mcrypt_create_iv(mcrypt_get_iv_size($this->cipher, $this->mode), MCRYPT_RAND);
67
            return openssl_random_pseudo_bytes(openssl_cipher_iv_length($this->method));
68
        }
69
        /**
70
 
71
         * @throws \Exception
72
         */
73
        public function encrypt() {
74
            if ($this->validateParams()) {
75
                return trim(openssl_encrypt($this->data, $this->method, $this->key, $this->options,$this->getIV()));
76
            } else {
77
                throw new \Exception('Invlid params!');
78
            }
79
        }
80
 
81
        /**
82
         *
83
         * @throws \Exception
84
         */
85
        public function decrypt() {
86
            if ($this->validateParams()) {
87
                $ret=openssl_decrypt($this->data, $this->method, $this->key, $this->options,$this->getIV());
88
 
89
                return   trim($ret);
90
            } else {
91
                throw new \Exception('Invlid params!');
92
            }
93
        }
94
    }