| 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( |
|
| 42 | |||
| 43 | 1 | public function encrypt($value) { |
|
| 53 | |||
| 54 | 1 | public function decrypt($value) { |
|
| 63 | |||
| 64 | 2 | public function iv() |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Generate IV |
||
| 71 | * |
||
| 72 | * @return int Returns a string of pseudo-random bytes, with the number of bytes expected by the method AES-256-CBC |
||
| 73 | */ |
||
| 74 | 1 | public static function generateIV() |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Generate a key |
||
| 82 | * |
||
| 83 | * @param int $length The length of the desired string of bytes. Must be a positive integer. |
||
| 84 | * |
||
| 85 | * @return int Returns the hexadecimal representation of a binary data |
||
| 86 | */ |
||
| 87 | 1 | public static function generateKey($length = 512) |
|
| 92 | } |
||
| 93 |