protected function encrypt($data, $key)
{
$iv = random_bytes(16);
// AES block size in CBC mode
// Encryption
$ciphertext = openssl_encrypt($data, 'AES-256-CBC', mb_substr($key, 0, 32, '8bit'), OPENSSL_RAW_DATA, $iv);
// Authentication
$hmac = hash_hmac('SHA256', $iv . $ciphertext, mb_substr($key, 32, null, '8bit'), true);
return $hmac . $iv . $ciphertext;
}