Created
February 23, 2017 09:24
-
-
Save troelskn/b2502a84a5f6a1f0332d5b6bb33471a5 to your computer and use it in GitHub Desktop.
Laravel workaround for broken openssl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// app/Providers/AppServiceProvider.php | |
namespace App\Providers; | |
use Illuminate\Support\ServiceProvider; | |
class AppServiceProvider extends ServiceProvider | |
{ | |
public function register() | |
{ | |
$this->app->singleton( | |
'encrypter', | |
function ($app) { | |
$config = $app->make('config')->get('app'); | |
// If the key starts with "base64:", we will need to decode the key before handing | |
// it off to the encrypter. Keys may be base-64 encoded for presentation and we | |
// want to make sure to convert them back to the raw bytes before encrypting. | |
if (\Illuminate\Support\Str::startsWith($key = $config['key'], 'base64:')) { | |
$key = base64_decode(substr($key, 7)); | |
} | |
return new \App\Encrypter($key, $config['cipher']); | |
} | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// app/Encrypter.php | |
namespace App; | |
use Illuminate\Encryption\Encrypter as BaseEncrypter; | |
use Illuminate\Encryption\EncryptException; | |
class Encrypter extends BaseEncrypter | |
{ | |
/** | |
* Encrypt the given value. | |
* | |
* @param string $value | |
* @return string | |
* | |
* @throws \Illuminate\Contracts\Encryption\EncryptException | |
*/ | |
public function encrypt($value) | |
{ | |
//$iv = random_bytes(16); | |
$iv = null; | |
$value = @\openssl_encrypt(serialize($value), $this->cipher, $this->key, 0, $iv); | |
if ($value === false) { | |
throw new EncryptException('Could not encrypt the data.'); | |
} | |
// Once we have the encrypted value we will go ahead base64_encode the input | |
// vector and create the MAC for the encrypted value so we can verify its | |
// authenticity. Then, we'll JSON encode the data in a "payload" array. | |
$mac = $this->hash($iv = base64_encode($iv), $value); | |
$json = json_encode(compact('iv', 'value', 'mac')); | |
if (! is_string($json)) { | |
throw new EncryptException('Could not encrypt the data.'); | |
} | |
return base64_encode($json); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment