rbaaboud/datasafe

提供加密和解密API,以保障数据安全的服务

1.0.2 2018-05-14 14:32 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:14:56 UTC


README

提供加密和解密API以保障数据安全的服务。

安装

通过Composer

$ composer require rbaaboud/datasafe

设置

设置 / 应用参数

<?php

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'
        ]
    ]
];

依赖 / 服务

在容器中注册服务。

工厂确保存在有效的设置并运行验证器。

<?php

/**
 * @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);
};

使用方法

<?php

// 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'
]);

测试

$ composer test