1 | <?php |
||
5 | class OpenCrypt |
||
6 | { |
||
7 | /** |
||
8 | * The cipher method. For a list of available cipher methods, use openssl_get_cipher_methods() |
||
9 | */ |
||
10 | const CIPHER_METHOD = "AES-256-CBC"; |
||
11 | |||
12 | /** |
||
13 | * When OPENSSL_RAW_DATA is specified, the returned data is returned as-is. |
||
14 | */ |
||
15 | const OPTIONS = OPENSSL_RAW_DATA; |
||
16 | |||
17 | /** |
||
18 | * The key |
||
19 | * |
||
20 | * Should have been previously generated in a cryptographically safe way, like openssl_random_pseudo_bytes |
||
21 | */ |
||
22 | private $secretKey; |
||
23 | |||
24 | /** |
||
25 | * IV - A non-NULL Initialization Vector. |
||
26 | * |
||
27 | * Encrypt method AES-256-CBC expects 16 bytes - else you will get a warning |
||
28 | */ |
||
29 | private $iv; |
||
30 | |||
31 | 2 | public function __construct( |
|
39 | |||
40 | 1 | public function encrypt(string $value) { |
|
50 | |||
51 | 1 | public function decrypt(string $value) { |
|
60 | |||
61 | 2 | public function iv() |
|
65 | |||
66 | /** |
||
67 | * Generate IV |
||
68 | * |
||
69 | * @return int Returns a string of pseudo-random bytes, with the number of bytes expected by the method AES-256-CBC |
||
70 | */ |
||
71 | 1 | public static function generateIV() |
|
76 | |||
77 | /** |
||
78 | * Generate a key |
||
79 | * |
||
80 | * @param int $length The length of the desired string of bytes. Must be a positive integer. |
||
81 | * |
||
82 | * @return int Returns the hexadecimal representation of a binary data |
||
83 | */ |
||
84 | 1 | public static function generateKey($length = 512) |
|
89 | } |
||
90 |