Skip to content

Use OpenSSL instead of Mcrypt in the examples #7934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions session/proxy_examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,22 @@ guest sessions.
Encryption of Session Data
--------------------------

If you wanted to encrypt the session data, you could use the proxy to encrypt
and decrypt the session as required::
If you want to encrypt the session data, you can use the proxy to encrypt and
decrypt the session as required. The following example uses the `php-encryption`_
library, but you can adapt it to any other library that you may be using::

// src/AppBundle/Session/EncryptedSessionProxy.php
namespace AppBundle\Session;

use Defuse\Crypto\Crypto;
use Defuse\Crypto\Key;
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;

class EncryptedSessionProxy extends SessionHandlerProxy
{
private $key;

public function __construct(\SessionHandlerInterface $handler, $key)
public function __construct(\SessionHandlerInterface $handler, Key $key)
{
$this->key = $key;

Expand All @@ -110,12 +113,12 @@ and decrypt the session as required::
{
$data = parent::read($id);

return mcrypt_decrypt(\MCRYPT_3DES, $this->key, $data);
return Crypto::decrypt($data, $this->key);
}

public function write($id, $data)
{
$data = mcrypt_encrypt(\MCRYPT_3DES, $this->key, $data);
$data = Crypto::encrypt($data, $this->key);

return parent::write($id, $data);
}
Expand Down Expand Up @@ -154,3 +157,5 @@ can intercept the session before it is written::
return parent::write($id, $data);
}
}

.. _`php-encryption`: https://github.com/defuse/php-encryption