superrb/encrypter-bundle

此包已被弃用且不再维护。没有建议的替代包。
关于此包的最新版本(dev-master)没有可用的许可证信息。

为Symfony2提供易于使用的加密服务

安装: 19

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 9

类型:symfony-bundle

dev-master 2016-05-26 16:33 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:58:36 UTC


README

PierrreEncrypterBundle在Symfony2中提供易于使用的加密服务。

特性

  • 可以从PHP代码中调用的服务
  • 可选的Twig扩展
  • 100% 单元测试! Build Status

先决条件

请确保已安装PHP Mcrypt扩展。

不要在生产环境中使用

这个库不适用于生产环境使用(因此没有稳定的标签)。它没有经过加密专家的审核。

安装

此安装过程旨在与Symfony2一起使用,但您可以通过子模块进行修改。

步骤 1:下载包

请在您的 deps 文件中添加以下行

[PierrreEncrypterBundle]
    git=http://github.com/pierrre/PierrreEncrypterBundle.git
    target=/bundles/Pierrre/EncrypterBundle

现在,运行 vendors 脚本来下载包

php bin/vendors install

步骤 2:配置自动加载器

将Pierrre命名空间添加到您的自动加载器中

<?php
// app/autoload.php
$loader->registerNamespaces(array(
    // ...
    'Pierrre' => __DIR__.'/../vendor/bundles',
));

步骤 3:启用包

最后,在kernel中启用包

<?php
// app/AppKernel.php
public function registerBundles()
{
    $bundles = array(
        // ...
        new Pierrre\EncrypterBundle\PierrreEncrypterBundle(),
    );
}

配置

这是默认配置,所有值都是可选的

# app/config.yml
pierrre_encrypter:
    encrypters: #Encrypters list, requires at least one encrypter.
        my_encrypter: #Encrypter name
            key: "@kernel.secret" #The secret that is used to encrypt data. By default, it will use the kernel secret.
            algorithm: "rijndael-128" #Encryption algorithm
            mode: "cbc" #Encryption mode
            random_initialization_vector: true #If you set it to false, it will use a blank string as initialization vector.
            base64: true #Encode the encrypted data with the base64 algorithm.
            base64_url_safe: true #Replace "+" and "/" characters by "-" and "_".
    twig: #Twig extension
        enabled: false #Enable extension
        default_encrypter: null #Default encrypter. By default, it's the first encrypter

请参阅Mcrypt文档

用法

请注意

  • 如果使用随机初始化向量,它将被连接到加密数据的开始处。
  • 解密数据末尾的"\0"字符将被移除。
  • 加密+base64数据末尾的"="字符将被移除。

编程方式

使用依赖注入容器

<?php
$encrypter = $container->get('pierrre_encrypter.manager')->get('my_encrypter');
$data = 'foobar';
$encryptedData = $encrypter->encrypt($data);
$decryptedData = $encrypter->decrypt($encryptedData);

手动

<?php
use Pierrre\EncrypterBundle\Util\Encrypter;
$encrypter = new Encrypter(array('key' => 'mySecret'));

Twig扩展

{{ data | encrypt }}
{{ data | encrypt('my_encrypter') }}