assistenzde/simple-cryptographic-bundle
The SimpleCryptographicService class contains method to encrypt/decrypt phrases with symmetric encryption. With the usage of OpenSSL each encoding of the same phrase results in a different encoded string.
Quick example usage:
$cryptographicService = new SimpleCryptographicService('$ecr3t');
$encryptedString1 = $cryptographicService->encrypt('Hello world!'); // some encrypted string, i.e. 'ABCDEFG'
echo $cryptographicService->decrypt($encryptedString1); // outputs 'Hello world!'
$encryptedString2 = $cryptographicService->encrypt('Hello world!'); // some encrypted string, i.e. 'HIJKLMNOP'
echo $cryptographicService->decrypt($encryptedString2); // outputs 'Hello world!'
This library can be used easily in Symfony projects but also in non-Symfony projects.
The usage of PHP ≥ v7.4 and Symfony ≥ 5 is recommended.
Please install via composer.
composer require assistenzde/simple-cryptographic-bundle
The bundle will be automatically added to your bundles.yaml configuration.
By default the bundle depends on the APP_SECRET environment variable and uses the aes-256-ctr cipher method. If you want to use a custom cipher key OR a customer cipher method create the
config/packates/simple-cryptographic-bundle.yaml configuration file and set the related values.
simple-cryptographic-bundle.yaml:
simple-cryptographic-bundle.yaml:
# set a custom cipher key or comment out to use the default value
# default is %kernel.secret% (which contains the APP_SECRET value)
key: My-cu5t0m-c1ph3r-k3y
# set a custom cipher method or comment out to use the default value
# default is aes-256-ctr
cipher: camellia-128-ofb
Use dependency injection to access the cryptographic service.
<?php
namespace MyNamespace;
use Assistenzde\SimpleCryptographicBundle\Service\SimpleCryptographicService;
/**
* Class AuthenticationSubscriber
*/
final class MyClass
{
/**
* the crypto service to encode/dedoce strings
*
* @var SimpleCryptographicService
*/
protected SimpleCryptographicService $simpleCryptographicService;
/**
* MyClass constructor
*
* @param SimpleCryptographicService $simpleCryptographicService
*/
public function __construct(SimpleCryptographicService $simpleCryptographicService)
{
$this->simpleCryptographicService = $simpleCryptographicService;
}
/**
*
*/
public function doSomething()
{
// do some calculation and get a user token
// encrypt the user token
$encryptedUserToken = $this->simpleCryptographicService->encrypt($userToken);
// and do some more stuff,
// esp. use the encrypted token (i.e. as request parameter or to save in a file)
// derypt the encrypted stuff
$userToken = $this->simpleCryptographicService->decrypt($encryptedUserToken);
}
}
We suggest using the public methods
To temporarily use custom settings switch to the static methods
A big thank you to stackoverflow's user Scott Arciszewski for the explanations in of the https://stackoverflow.com/a/30189841/4351778 answer.
How can I help you explore Laravel packages today?