-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncryption.php
49 lines (43 loc) · 1.42 KB
/
Encryption.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
Class Encryption{
/**
* Encode - Encrypt string
*
* @param string $input - String to encode
* @param string $key - Secret key
* @param string $enc_method (optional)
*
* @return string Encrypted string
*/
static function Encode(string $input, string $key, string $enc_method = 'AES-256-CBC'){
$enc_iv = self::_generateIV($key, openssl_cipher_iv_length($enc_method));
return base64_encode(openssl_encrypt($input, $enc_method, $key, 0, $enc_iv));
}
/**
* Decode - Decrypt encrypted string
*
* @param string $input - Encrypted string
* @param string $key - Secret key
* @param string $enc_method (optional)
*
* @return string Decoded string
*/
static function Decode(string $input, string $key, string $enc_method = 'AES-256-CBC'){
$enc_iv = self::_generateIV($key, openssl_cipher_iv_length($enc_method));
return openssl_decrypt(base64_decode($input), $enc_method, $key, 0, $enc_iv);
}
/**
* _generateIV - Automatic generate rquired encrypion iv from key
*
* @param string $key - Secret key
* @param int $size - Size of the iv
* @return string iv
*/
static function _generateIV($key, $size){
$hash = base64_encode(md5($key));
while(strlen($hash) < $size){
$hash = $hash.$hash;
}
return substr($hash, 0, $size);
}
}