1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Encryption and Decryption Class |
5
|
|
|
* |
6
|
|
|
*/ |
7
|
|
|
class Encryption |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Cipher algorithm |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
const CIPHER = 'aes-256-cbc'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Hash function |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
const HASH_FUNCTION = 'sha256'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* constructor for Encryption object. |
26
|
|
|
* |
27
|
|
|
* @access private |
28
|
|
|
*/ |
29
|
|
|
private function __construct() |
30
|
|
|
{ |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Encrypt a string. |
35
|
|
|
* |
36
|
|
|
* @access public |
37
|
|
|
* @static static method |
38
|
|
|
* @param string $plain |
39
|
|
|
* @return string |
40
|
|
|
* @throws Exception If functions don't exists |
41
|
|
|
*/ |
42
|
|
|
public static function encrypt($plain) |
43
|
|
|
{ |
44
|
|
|
if (!function_exists('openssl_cipher_iv_length') || |
45
|
|
|
!function_exists('openssl_random_pseudo_bytes') || |
46
|
|
|
!function_exists('openssl_encrypt')) { |
47
|
|
|
|
48
|
|
|
throw new Exception('Encryption function doesn\'t exist'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// generate initialization vector, |
52
|
|
|
// this will make $iv different every time, |
53
|
|
|
// so, encrypted string will be also different. |
54
|
|
|
$iv_size = openssl_cipher_iv_length(self::CIPHER); |
55
|
|
|
$iv = openssl_random_pseudo_bytes($iv_size); |
56
|
|
|
|
57
|
|
|
// generate key for authentication using ENCRYPTION_KEY & HMAC_SALT |
58
|
|
|
$key = mb_substr(hash(self::HASH_FUNCTION, Config::get('ENCRYPTION_KEY') . Config::get('HMAC_SALT')), 0, 32, '8bit'); |
59
|
|
|
|
60
|
|
|
// append initialization vector |
61
|
|
|
$encrypted_string = openssl_encrypt($plain, self::CIPHER, $key, OPENSSL_RAW_DATA, $iv); |
62
|
|
|
$ciphertext = $iv . $encrypted_string; |
63
|
|
|
|
64
|
|
|
// apply the HMAC |
65
|
|
|
$hmac = hash_hmac('sha256', $ciphertext, $key); |
66
|
|
|
|
67
|
|
|
return $hmac . $ciphertext; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Decrypted a string. |
72
|
|
|
* |
73
|
|
|
* @access public |
74
|
|
|
* @static static method |
75
|
|
|
* @param string $ciphertext |
76
|
|
|
* @return string |
77
|
|
|
* @throws Exception If $ciphertext is empty, or If functions don't exists |
78
|
|
|
*/ |
79
|
|
|
public static function decrypt($ciphertext) |
80
|
|
|
{ |
81
|
|
|
if (empty($ciphertext)) { |
82
|
|
|
throw new Exception('The String to decrypt can\'t be empty'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (!function_exists('openssl_cipher_iv_length') || |
86
|
|
|
!function_exists('openssl_decrypt')) { |
87
|
|
|
|
88
|
|
|
throw new Exception('Encryption function doesn\'t exist'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// generate key used for authentication using ENCRYPTION_KEY & HMAC_SALT |
92
|
|
|
$key = mb_substr(hash(self::HASH_FUNCTION, Config::get('ENCRYPTION_KEY') . Config::get('HMAC_SALT')), 0, 32, '8bit'); |
93
|
|
|
|
94
|
|
|
// split cipher into: hmac, cipher & iv |
95
|
|
|
$macSize = 64; |
96
|
|
|
$hmac = mb_substr($ciphertext, 0, $macSize, '8bit'); |
97
|
|
|
$iv_cipher = mb_substr($ciphertext, $macSize, null, '8bit'); |
98
|
|
|
|
99
|
|
|
// generate original hmac & compare it with the one in $ciphertext |
100
|
|
|
$originalHmac = hash_hmac('sha256', $iv_cipher, $key); |
101
|
|
|
if (!self::hashEquals($hmac, $originalHmac)) { |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// split out the initialization vector and cipher |
106
|
|
|
$iv_size = openssl_cipher_iv_length(self::CIPHER); |
107
|
|
|
$iv = mb_substr($iv_cipher, 0, $iv_size, '8bit'); |
108
|
|
|
$cipher = mb_substr($iv_cipher, $iv_size, null, '8bit'); |
109
|
|
|
|
110
|
|
|
return openssl_decrypt($cipher, self::CIPHER, $key, OPENSSL_RAW_DATA, $iv); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* A timing attack resistant comparison. |
115
|
|
|
* |
116
|
|
|
* @access private |
117
|
|
|
* @static static method |
118
|
|
|
* @param string $hmac The hmac from the ciphertext being decrypted. |
119
|
|
|
* @param string $compare The comparison hmac. |
120
|
|
|
* @return bool |
121
|
|
|
* @see https://github.com/sarciszewski/php-future/blob/bd6c91fb924b2b35a3e4f4074a642868bd051baf/src/Security.php#L36 |
122
|
|
|
*/ |
123
|
|
|
private static function hashEquals($hmac, $compare) |
124
|
|
|
{ |
125
|
|
|
if (function_exists('hash_equals')) { |
126
|
|
|
return hash_equals($hmac, $compare); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// if hash_equals() is not available, |
130
|
|
|
// then use the following snippet. |
131
|
|
|
// It's equivalent to hash_equals() in PHP 5.6. |
132
|
|
|
$hashLength = mb_strlen($hmac, '8bit'); |
133
|
|
|
$compareLength = mb_strlen($compare, '8bit'); |
134
|
|
|
|
135
|
|
|
if ($hashLength !== $compareLength) { |
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$result = 0; |
140
|
|
|
for ($i = 0; $i < $hashLength; $i++) { |
141
|
|
|
$result |= (ord($hmac[$i]) ^ ord($compare[$i])); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $result === 0; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|