Skip to content

Instantly share code, notes, and snippets.

@RDelorier
Last active January 20, 2016 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RDelorier/f1097951a1b25358e43c to your computer and use it in GitHub Desktop.
Save RDelorier/f1097951a1b25358e43c to your computer and use it in GitHub Desktop.
Laravel compatible encrypt method
<?php
/**
* Encrypts the value in a way compatible with laravel 5.1
* @param $value value to encrypt
*
* @return string
*/
function encrypt($value)
{
$key = 'secret-key-here';
$cipher = 'AES-256-CBC';
$iv = openssl_random_pseudo_bytes(16);
$value = openssl_encrypt(serialize($value), $cipher, $key, 0, $iv);
$iv = base64_encode($iv);
$mac = hash_hmac('sha256', $iv . $value, $key);
$json = json_encode(compact('iv', 'value', 'mac'));
return base64_encode($json);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment