PHP code example of dbublik / cryptography

1. Go to this page and download the library: Download dbublik/cryptography library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dbublik / cryptography example snippets


use DBublik\Cryptography\Encrypter;

$secretKey = 'your_secret_key';
$encrypter = Encrypter::create($secretKey);

// config/services.php

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use DBublik\Cryptography\Encrypter;

return function(ContainerConfigurator $container): void {
    $services = $container->services();

    $services->set(Encrypter::class)
        ->factory([null, 'create'])
        ->args([env('YOUR_SECRET_KEY')]);
};

final readonly class ExampleService
{
    public function __construct(
        private \DBublik\Cryptography\Encrypter $encrypter,
    ) {}

    public function save(#[\SensitiveParameter] string $sensitiveValue): void
    {
        $encryptedValue = $this->encrypter->encrypt($sensitiveValue);

        // Don't forget to save $encryptedValue somewhere
    }
}

final readonly class ExampleService
{
    public function __construct(
        private \DBublik\Cryptography\Encrypter $encrypter,
    ) {}

    public function doSomething(string $encryptedValue): mixed
    {
        $sensitiveValue = $this->encrypter->decrypt($encryptedValue);

        // Be careful! Do not show $sensitiveValue to anyone
    }
}