grandmasterx / encryptdectypt
加密-解密 API
Requires
- php: >=5.5.9
Suggests
- ext-libsodium: Install php-libsodium if you want to use SodiumCipher with higher performance and use Argon2 or Scrypt password hash.
- paragonie/sodium_compat: Install paragonie/sodium_compat if you want to SodiumCipher.
This package is not auto-updated.
Last update: 2024-09-15 04:55:58 UTC
README
Windwalker Crypt 包是 PHP Openssl 和 Libsodium 库的包装,用于散列和验证密码,并提供一个简单的接口来执行对称算法加密。
通过 Composer 安装
将此添加到您的 composer.json
文件中的 require 块。
{ "require": { "windwalker/crypt": "~3.0" } }
密码散列
Password
对象是一个简单的对象,用于加密用户的密码,无法解密密码散列,Password
对象使用单向算法。
创建密码
use Windwalker\Crypt\Password; $password = new Password; $pass = $password->create('pass1234'); // $2y$10$csNfML/FJlKwaHR8xREgZuhp0pqSqeg.jdACqDsKO/MCHDkTuIZEa
使用其他散列算法
use Windwalker\Crypt\Password; $password = new Password(Password::SHA256); $pass = $password->create('pass1234');
设置成本和盐
use Windwalker\Crypt\Password; // The Blowfish algorithm should set cost number between 4 to 31. // We are suggest not higher than 15, else it will be too slow. $password = new Password(Password::BLOWFISH, 15, md5('to be or not to be.')); $pass = $password->create('pass1234'); // Note the Sha256 and Sha512 should set cost number higher than 1000 $password = new Password(Password::SHA512, 5000, md5('to be or not to be.')); $pass = $password->create('pass1234');
可用算法
- Password::MD5
- Password::BLOWFISH (默认)
- Password::SHA256
- Password::SHA512
- Password::ARGON2 (libsodium)
- Password::SCRYPT (libsodium)
注意:
ARGON2
和SCRYPT
必须先安装 phpext-libsodium
和 libsodium 库。原生 PHP 无法实现它们。这两个算法将忽略成本和盐,并使用 Sodium 方式散列密码。
验证密码
我们不需要关心散列算法,Password 对象将自动检测算法类型
$bool = $password->verify('pass1234', $pass);
对称密钥算法加密
Crypt
对象提供了不同的加密/解密数据的方式。这些加密方式中的大多数必须使用 PHP openssl 函数才能工作。如果您的 PHP 不支持 openssl 扩展,您可以使用 PhpAesCipher
作为默认加密方式,它是由 aes.class.php 实现的 AES。
使用加密方式
use Windwalker\Crypt\Cipher\BlowfishCipher; use Windwalker\Crypt\Crypt; $crypt = new Crypt(new BlowfishCipher); $encrypted = $crypt->encrypt('My Text', 'My private key'); $bool = $crypt->verify('My Text', $encrypted, 'My private key'); // True
获取明文
$crypt = new Crypt(new BlowfishCipher); $encrypted = $crypt->encrypt('My Text', 'My private key'); $text = $crypt->decrypt($encrypted);
自定义加密方式
您可以设置模式为加密方式。
$cipher = new BlowfishCipher($key); $cipher->setMode('ecb'); $cipher->encrypt(...);
或设置 PBKDF2 迭代次数。
$cipher = new BlowfishCipher($key, array('pbkdf2_iteration' => 64000)); // Default is 12000
Libsodium
使用 Libsodium 加密数据,您必须先安装 paragonie/sodium_compat。
paragonie/sodium_compat
帮助我们使用 libsodium 而不使用扩展,但您应该安装 ext-libsodium
来获得更高的性能。
use Windwalker\Crypt\Cipher\SodiumCipher; $crypt = new Crypt(new SodiumCipher); $encrypted = $cipher->encrypt($text); $text = $cipher->decrypt($encrypted);
纯 PHP 无法实现内存擦除,如果您收到 sodium_memzero() 仅支持 php 7.2 或安装 ext-libsodium 后
的消息,这意味着您必须安装 ext-libsodium
或使用 php 7.2 或更高版本,您也可以通过 ignoreMemzero()
禁用内存擦除(但我们不建议这样做)
$cipher = new SodiumCipher; $cipher->ignoreMemzero(true); $crypt = new Crypt($cipher);
可用加密方式
- BlowfishCipher
- Aes56Cipher
- Des3Cipher
- SodiumCipher - PHP Libsodium 加密方式,必须先安装
ext-libsodium
。 - PhpAesCipher - 仅在系统不支持 openssl 扩展时使用。