PHP code example of rbaaboud / datasafe

1. Go to this page and download the library: Download rbaaboud/datasafe 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/ */

    

rbaaboud / datasafe example snippets





return [
    'datasafe' => [
        'crypt' => [
            // method, see openssl_get_cipher_methods()
            'method' => 'AES-256-CFB',
            
            // key, see openssl_encrypt()
            'key' => 'vr6EkKQ8S8vuxnchzKJbmqPUbkm9mX5L',
            
            // options, see openssl_encrypt()
            'options' => 0,
            
            // iv, see openssl_encrypt() and openssl_cipher_iv_length()
            'iv' => 'cvXKhMvNk2LEYpG2'
        ]
    ]
];





/**
 * @param \Psr\Container\ContainerInterface $container
 * @return \Rbaaboud\DataSafe\Crypt
 */
$container['datasafe'] = function (\Psr\Container\ContainerInterface $container) {
    $factory = new \Rbaaboud\DataSafe\Crypt\Factory();

    // settings
    $settingsService = $container->get('settings');

    // formatter
    // or a custom formatter that implements \Rbaaboud\DataSafe\Formatter\FormatterInterface
    $formatter = new \Rbaaboud\DataSafe\Formatter();

    return $factory($settingsService['datasafe']['crypt'], $formatter);
};





// get datasafe service
/** @var \Rbaaboud\DataSafe\Crypt $datasafeService */
$datasafeService = $this->container->get('datasafe');

// crypt

// returns crypted string
$datasafeService->crypt('test');

// cryptArray

// returns array with all values crypted
$datasafeService->cryptArray([
    'foo',
    'bar'
]);

// returns array with only 'index2' value crypted
$datasafeService->cryptArray([
    'index1' => 'foo',
    'index2' => 'bar'
], [
    'index2'
]);

// uncrypt

// returns uncrypted string
$datasafeService->uncrypt('d91wtw==');

// uncryptArray

// returns array with all values uncrypted
$datasafeService->uncryptArray([
    'Zdds',
    'd91wtw=='
]);

// returns array with only 'index2' value uncrypted
$datasafeService->uncryptArray([
    'index1' => 'Zdds',
    'index2' => 'd91wtw=='
], [
    'index2'
]);