athens/encryption

此软件包最新版本(0.3.3)没有提供许可信息。

Propel行为,实现数据列的无缝加密/解密

安装: 546

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 3

类型:propel-behavior

0.3.3 2016-02-26 17:04 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:03:58 UTC


README

Build Status Code Climate Test Coverage Latest Stable Version

Athens\Encryption

无缝加密/解密 Propel2 数据字段。此库是Propel2 ORM 框架的一个插件

例如

// schema.xml

    <table name="my_class">
        <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>

        <column name="my_data" type="varchar" size="255" />
        <column name="my_secret_data" type="varbinary" size="255" />

        <behavior name="encryption">
            <parameter name="column_name_1" value="my_secret_data" />
        </behavior>
    </table>


// Before any database queries:

    use Athens\Encryption\Cipher;
    Cipher::createInstance("mysecretpassphrase");


// In your program:

    $o = new MyClass();

    $o->setMyData("Some data that will remain as plain text.");
    $o->setMySecretData("Some data that will be encrypted.");

    $o->save();

// Later:

    $o = MyClassQuery::create()->findOneByMyData("Some data that will remain as plain text.");

    echo $o->getMySecretData();
    // "Some data that will be encrypted."

给定上面的表定义,字符串 "Some data that will be encrypted." 在发送到数据库之前在内存中被加密。当我们稍后检索 MySecretData 时,密文在返回之前被解密。

注意/权衡

Athens/Encryption 破坏了Propel在加密字段上的本地搜索/查找/排序方法。因为加密字段的明文不可用给数据库,所以没有数据库搜索或排序方法可以操作这些字段。搜索或排序只能通过检索所有行,解密所有值,然后在这些值上执行搜索/排序来完成。如果您有大量行并且需要搜索/排序加密字段,这个过程可能非常慢。

安装

此库在 Packagist 上发布。要使用 Composer 安装,请将 "Athens/Encryption": "0.1.*" 行添加到您的 "require" 依赖中

{
    "require": {
        "Athens/Encryption": ">=0.1"
    }
}

当然,如果您不使用 Composer,则可以通过右侧的 下载 ZIP 按钮下载仓库。

使用

此客户端库提供 Cipher 类和一 Propel2 行为类。

要在您的 Propel 模式中指定一个字段为加密,请将其类型设置为 varbinary 并包含 encryption 行为。您可以在 encryption 行为中包含多个列

    <table name="my_class">
        <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>

        <column name="my_data" type="varchar" size="255" />

        <column name="my_secret_data" type="varbinary" size="255" />
        <column name="my_secret_data2" type="varbinary" size="255" />

        <behavior name="encryption">
            <parameter name="column_name_1" value="my_secret_data" />
            <parameter name="column_name_2" value="my_secret_data2" />
        </behavior>
    </table>

然后像平常一样构建您的模型和数据库。

在查询数据库之前,您必须使用您的密码短语初始化 Cipher 类

    // Intialize the cipher
    Cipher::createInstance($my_passphrase);

参数 $my_passphrase 应该是一串随机字符。32-64 个字符的长度是密码短语合适的长度。因为加密每次页面加载时都会初始化,所以密码短语必须存储在您的服务器上,并且对 PHP 可访问。然而,密码短语不应该在可被网络访客查看的文件中,而且几乎肯定不应该包含在您的源代码/版本控制(git,scm 等)中。

就这样!现在 MySecretDataMySecretData2 的类设置器在发送到数据库之前无缝加密它们的数据。现在 MySecretDataMySecretData2 的类获取器在从数据库检索数据之后无缝解密数据。

请记住,由于上述原因,现在搜索/查找和排序对于 MySecretDataMySecretData2 已经是损坏的。

兼容性

  • PHP 5.5, 5.6, 7.0
  • Propel2

待办事项

请参阅 GitHub 问题跟踪器

参与

请随时打开拉取请求或问题。 GitHub 是此项目的官方位置。

以下是代码贡献的一般事件顺序

  1. 问题跟踪器 中打开一个问题。
  2. 按任何顺序
  • 提交一个包含失败测试的拉取请求,以演示问题/功能。
  • 获取确认/同意。
  1. 修订您的拉取请求,以通过(2)中的测试。如果适当,包括文档。