· 7 years ago · Mar 19, 2018, 03:42 AM
1// En archivo de configuración kumbiaPHP default/app/config
2//
3// crear la variable secret_iv bajo authentication, por ejemplo:
4//
5// [authentication]
6// secret_key = "z?&vbX$4\N`}@vo=~)\?eBFyc&;J.Nx-58/dU8\)"
7// copy/paste este código para un controlador de acceso global
8//
9 const ENCRYPT_METHOD = 'AES-256-CBC';
10 const SECRET_IV = 'MAPt=&L3>*m@=K;UyU`2fGcT\e[#{LiUha@_/yF%';
11 const SHA256 = 'sha256';
12public static function stringEncode($string)
13 {
14
15 $encrypt_method = self::ENCRYPT_METHOD;
16 $secret_iv = self::SECRET_IV;
17 $secret_key = config::get("config.authentication.secret_key");
18
19
20 // hash
21 $key = hash(self::SHA256, $secret_key);
22
23 // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
24 $iv = substr(hash(self::SHA256, $secret_iv), 0, 16);
25 $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
26
27 return base64_encode($output);
28 }
29
30 public static function stringDecode($string)
31 {
32
33 $encrypt_method = self::ENCRYPT_METHOD;
34 $secret_iv = self::SECRET_IV;
35 $secret_key = config::get("config.authentication.secret_key");
36
37
38 // hash
39 $key = hash(self::SHA256, $secret_key);
40
41 // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
42 $iv = substr(hash(self::SHA256, $secret_iv), 0, 16);
43
44 return openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
45
46 }