Skip to content

Instantly share code, notes, and snippets.

@cviebrock
Last active October 8, 2021 17:31
Show Gist options
  • Save cviebrock/136f0b82c87cbc8455a720f87f6e07a3 to your computer and use it in GitHub Desktop.
Save cviebrock/136f0b82c87cbc8455a720f87f6e07a3 to your computer and use it in GitHub Desktop.
Replacement for Laravel 3.2's crypter class, that doesn't require mcrypt
<?php namespace Laravel; defined('DS') or die('No direct script access.');
/**
* The mcrypt library was deprecated in PHP7.2, so I couldn't run old Laravel 3.2
* applications anymore. Rather than upgrade the whole application, you can just
* replace the `/laravel/crypter.php` file with this one, which uses the
* OpenSSL library instead of mcrypt.
*/
class Crypter {
/**
* The encryption cipher.
*
* @var string
*/
public static $cipher = 'aes-256-cbc-hmac-sha256';
/**
* Encrypt a string.
*
* The string will be encrypted using the AES-256 scheme and will be base64 encoded.
*
* @param string $value
* @return string
*/
public static function encrypt($value)
{
$iv = openssl_random_pseudo_bytes(static::iv_size());
$value = openssl_encrypt($value, static::$cipher, static::key(), OPENSSL_RAW_DATA, $iv);
return base64_encode($iv.$value);
}
/**
* Decrypt a string.
*
* @param string $value
* @return string
*/
public static function decrypt($value)
{
$value = base64_decode($value);
// To decrypt the value, we first need to extract the input vector and
// the encrypted value. The input vector size varies across different
// encryption ciphers and modes, so we'll get the correct size.
$iv = substr($value, 0, static::iv_size());
$value = substr($value, static::iv_size());
$value = openssl_decrypt($value, static::$cipher, static::key(), OPENSSL_RAW_DATA, $iv);
return $value;
}
/**
* Get the input vector size for the cipher and mode.
*
* @return int
*/
protected static function iv_size()
{
return openssl_cipher_iv_length(static::$cipher);
}
/**
* Get the encryption key from the application configuration.
*
* @return string
*/
protected static function key()
{
return Config::get('application.key');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment