ezimuel/phpcrypto

基于 OpenSSL 的 PHP 7 加密库

dev-master 2016-04-08 11:14 UTC

This package is auto-updated.

Last update: 2024-08-24 00:04:40 UTC


README

Build Status

关于

这是一个 PHP 7 的加密库。它基于 OpenSSL 并提供以下功能

  • 对称加密和认证(默认为 AES + HMAC-SHA256);
  • 公钥加密(密钥管理,加密/解密)
  • 使用对称和公钥的混合加密(类似于 OpenPGP

版本

由于此软件是 ALPHA,使用风险自担!

使用方法

使用方法非常简单,安装库后使用 composer

composer require ezimuel/phpcrypto:dev-master

您可以使用 Symmetric、PublicKey 和 Hybrid 类来消费对称加密、公钥和混合加密。

例如,如果您想以对称方式加密字符串,可以使用以下代码

use PHPCrypto\Symmetric;

$plaintext = 'Text to encrypt';
$key = '123456789012'; // This can be also a user's password we generate a new
                       // one for encryption using PBKDF2 algorithm

$cipher = new Symmetric(); // AES + HMAC-SHA256 by default
$cipher->setKey($key);
$ciphertext = $cipher->encrypt($plaintext);

// or passing the $key as optional paramter
// $ciphertext = $cipher->encrypt($plaintext, $key);

$result = $cipher->decrypt($ciphertext);

// or passing the $key as optional paramter
// $result = $cipher->decrypt($ciphertext, $key);

print ($result === $plaintext) ? "OK" : "FAILURE";

安全最佳实践

在此项目中,我们使用了以下安全最佳实践

待办事项

  • PublicKey 中的加密/解密函数
  • PublicKey 中的数字签名签名/验证函数
  • 在混合模式中支持多个密钥
  • 公钥模式中的 CA 管理

关于 OpenSSL 扩展的注意事项

在此,我报告了一些关于 OpenSSL PHP 扩展使用的情况

  • 最好有 openssl_cipher_key_size() 函数来获取特定加密算法的密钥大小;

版权

版权 2016 由 Enrico Zimuel 所有

许可证的使用在 LICENSE 文件中报告。