交集/lapis

CakePHP插件,提供高度安全的多层公钥数据库加密,用于用户级。

安装: 37

依赖: 0

建议者: 0

安全: 0

星级: 0

观察者: 7

分支: 0

开放问题: 3

类型:cakephp-plugin

dev-master 2017-04-18 03:30 UTC

This package is not auto-updated.

Last update: 2024-09-14 20:09:44 UTC


README

Build Status License

CakePHP 2.x 插件,提供高度安全的公钥加密,用于数据库I/O。

这是一个正在进行中的项目。实现、文档和示例代码不仅不完整,而且可能会在没有通知的情况下更改。

3.x 插件将很快推出。

Lapis 以东南亚的分层蛋糕“Kue lapis”命名。类似于蛋糕,Lapis 插件采用多层加密方案来保护您的数据安全。

动机

待办事项:为什么选择 Lapis

设置

  1. 将 Lapis 插件安装到您的本地 CakePHP 应用程序中。

  2. 运行以下命令创建必要的表:

    Console/cake schema create --plugin Lapis
  3. 按照引导密钥生成器生成根密钥对。您至少需要1个根密钥对才能使用 Lapis。

    Console/cake Lapis.keys generate    # follow the guided prompts

    强烈建议不要在数据库中存储未加密的私钥。如果您为私钥提供了密码,请注意密码不会存储在任何系统位置。您必须在系统外安全地单独存储密码。

示例模型

  1. 为了准备一个用于 Lapis 加密文档的模型,请向关联表中添加一个名为 document 的文本字段。

    ALTER TABLE `table_name` ADD `document` TEXT NULL DEFAULT NULL;

    document 字段之上,您仍然可以包含其他传统字段,如 idcreated 或自定义字段,如 title 等。请注意,传统字段中的数据将不会加密,但作为原生字段,它们将继续享受数据库级别的特权,如索引等。

  2. 更新您的模型以包含 Lapis.SecDoc 行为并定义文档架构。Lapis 支持以下 JSON 数据类型:stringnumberboolean。如果您不想强制数据类型,您可以将文档字段指定为 inherit 或使用非关联数组。

    以下是一个带有 Lapis 加密文档的 Book 模型示例。

    class Book extends AppModel {
    	public $name = 'Book';
    	public $actsAs = array('Lapis.SecDoc');
    
    	/**
    	 * Either number, string or boolean
    	 */
    	public $documentSchema = array(
    		'author' => 'string',
    		'pages' => 'number',
    		'available' => 'boolean'
    	);
    
    	// or if you prefer to not enforce JSON data types, you can list the schema as such
    	// public $documentSchema = array('author', 'pages', 'available');
    }
  3. 要保存到加密文档模型,您需要指定希望提供访问权限的 最低密钥。Lapis 将为所有指定的公钥及其相应的祖先密钥直到根密钥进行签名。

    $data = array(
    	// Conventional database fields
    	'title' => 'Book Title',
    
    	// Secured document
    	'author' => 'John Doe',
    	'pages' => 488,
    	'available' => true
    );
    
    $this->Book->saveFor = 2;
    $this->Book->saveFor = array(2, 5); // for multiple lowest keys
    
    $this->Book->create();
    $this->Book->save($data);

    假设密钥层次结构如图所示

    /*
     * 1 (root) => 2 => 9
     * 3 (root) => 4 => 5
     **/
    
    $this->Book->saveFor = 2;
    // would provide access to keys with IDs: 2 and 1 (its ancestors), but not 3 (even though it is a root key)
    
    $this->Book->saveFor = array(2, 5);
    // would provide access to keys with IDs: 2, 1; and 5, 4, 3.
  4. 要查询加密文档模型,您必须提供具有对文档特权访问权限的未加密私钥,或具有对文档特权访问权限的加密私钥的密码。

    // Specifying unencrypted private key in PEM encoded format, including header and footer.
    $this->Book->requestAs = array('id' => 2, 'unencrypted_key' => 'PEM_ENCODED_UNENCRYPTED_PRIVATE_KEY';
    
    // or, the password to an encrypted private key in `keys` table
    $this->Book->requestAs = array('id' => 23, 'password' => 'PASSWORD_TO_DECRYPT_PVT_KEY');
    
    // if private key is stored unencrypted in database (not recommended), id is all that is required.
    $this->Book->requestAs = array('id' => 23);
    
    $this->Book->find('first', array(
    	'conditons' => array('Book.id' => 2)
    ));
    
    // If the supplied private key has privileged access to the document, unencrypted document fields would be returned normally just like a normal database fields.
    // Otherwise, only database fields would be returned encrypted.

注意

  1. 强烈建议不要在数据库中存储未加密的私钥。