仓库初始化
This commit is contained in:
143
extend/Yzh/Utils/Des.php
Normal file
143
extend/Yzh/Utils/Des.php
Normal file
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
|
||||
namespace Yzh\Utils;
|
||||
|
||||
use Yzh\Exception\RunTimeException;
|
||||
use Yzh\Exception\ExceptionCode;
|
||||
|
||||
class Des
|
||||
{
|
||||
private $des3_key; // 密钥向量
|
||||
private $iv; // 混淆向量
|
||||
private $mode = 'cbc';
|
||||
|
||||
/**
|
||||
* 构造,传递二个已经进行 base64_encode 的 KEY 与 IV
|
||||
* @param $des3key
|
||||
* @param null $iv
|
||||
*/
|
||||
function __construct($des3key, $iv = null)
|
||||
{
|
||||
$this->des3_key = $des3key;
|
||||
$this->iv = $iv;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密
|
||||
* @param $value
|
||||
* @return string
|
||||
* @throws RunTimeException
|
||||
*/
|
||||
public function encrypt($value)
|
||||
{
|
||||
// 兼容5.3以前版本
|
||||
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
|
||||
return $this->encrypt5($value);
|
||||
}
|
||||
$iv = substr($this->des3_key, 0, 8);
|
||||
$ret = openssl_encrypt($value, 'DES-EDE3-CBC', $this->des3_key, 0, $iv);
|
||||
if (false === $ret) {
|
||||
$errMsg = "DES encrypt err";
|
||||
while ($errStr = openssl_error_string()) {
|
||||
$errMsg = $errStr;
|
||||
}
|
||||
throw new RunTimeException($errMsg, ExceptionCode::DES_KEY_ENCRYPT_FAIL);
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密
|
||||
* @param $value
|
||||
* @return bool|false|string
|
||||
* @throws RunTimeException
|
||||
*/
|
||||
public function decrypt($value)
|
||||
{
|
||||
// 兼容5.3以前版本
|
||||
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
|
||||
return $this->decrypt5($value);
|
||||
}
|
||||
$iv = substr($this->des3_key, 0, 8);
|
||||
$ret = openssl_decrypt($value, 'DES-EDE3-CBC', $this->des3_key, 0, $iv);
|
||||
if (false === $ret) {
|
||||
$errMsg = "DES decrypt err";
|
||||
while ($errStr = openssl_error_string()) {
|
||||
$errMsg = $errStr;
|
||||
}
|
||||
throw new RunTimeException($errMsg, ExceptionCode::DES_KEY_DECRYPT_FAIL);
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密 5.3以前版本
|
||||
* @param $value
|
||||
* @return bool|false|string
|
||||
*/
|
||||
private function encrypt5($value)
|
||||
{
|
||||
$td = mcrypt_module_open("tripledes", '', $this->mode, '');
|
||||
$iv = substr($this->des3_key, 0, 8);
|
||||
$value = $this->paddingPKCS7($value);
|
||||
if (false === $value) {
|
||||
return openssl_error_string();
|
||||
}
|
||||
@mcrypt_generic_init($td, $this->des3_key, $iv);
|
||||
$dec = mcrypt_generic($td, $value);
|
||||
$ret = base64_encode($dec);
|
||||
mcrypt_generic_deinit($td);
|
||||
mcrypt_module_close($td);
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密 5.3以前版本
|
||||
* @param $value
|
||||
* @return bool|false|string
|
||||
*/
|
||||
private function decrypt5($value)
|
||||
{
|
||||
$td = mcrypt_module_open("tripledes", '', $this->mode, '');
|
||||
$iv = substr($this->des3_key, 0, 8);
|
||||
@mcrypt_generic_init($td, $this->des3_key, $iv);
|
||||
$ret = trim(mdecrypt_generic($td, base64_decode($value)));
|
||||
$ret = $this->unPaddingPKCS7($ret);
|
||||
mcrypt_generic_deinit($td);
|
||||
mcrypt_module_close($td);
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* 填充
|
||||
* @param $value
|
||||
* @return bool|false|string
|
||||
*/
|
||||
private function paddingPKCS7($value)
|
||||
{
|
||||
$blockSize = mcrypt_get_block_size('tripledes', $this->mode);
|
||||
if (false === $blockSize) {
|
||||
return false;
|
||||
}
|
||||
$paddingChar = $blockSize - (strlen($value) % $blockSize);
|
||||
$value .= str_repeat(chr($paddingChar), $paddingChar);
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 去掉填充
|
||||
* @param $value
|
||||
* @return bool|false|string
|
||||
*/
|
||||
private function unPaddingPKCS7($value)
|
||||
{
|
||||
$pad = ord($value[strlen($value) - 1]);
|
||||
if ($pad > strlen($value)) {
|
||||
return false;
|
||||
}
|
||||
if (strspn($value, chr($pad), strlen($value) - $pad) != $pad) {
|
||||
return false;
|
||||
}
|
||||
return substr($value, 0, -1 * $pad);
|
||||
}
|
||||
}
|
||||
31
extend/Yzh/Utils/Hmac.php
Normal file
31
extend/Yzh/Utils/Hmac.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Yzh\Utils;
|
||||
|
||||
class Hmac
|
||||
{
|
||||
protected $mode = "sha256";
|
||||
protected $app_key;
|
||||
|
||||
/**
|
||||
* @param string $appKey
|
||||
*/
|
||||
public function __construct($appKey)
|
||||
{
|
||||
$this->app_key = $appKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $data
|
||||
* @param string $key
|
||||
*/
|
||||
public function sign($data): string
|
||||
{
|
||||
return hash_hmac($this->mode, $data, $this->app_key);
|
||||
}
|
||||
|
||||
public function verify($data, $sign)
|
||||
{
|
||||
return $this->sign($data) == $sign;
|
||||
}
|
||||
}
|
||||
20
extend/Yzh/Utils/MessString.php
Normal file
20
extend/Yzh/Utils/MessString.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Yzh\Utils;
|
||||
|
||||
class MessString
|
||||
{
|
||||
const MIN_RAND_LENGTH = 8;
|
||||
const DEFAULT_RAND_LENGTH = 16;
|
||||
const MAX_RAND_LENGTH = 64;
|
||||
const ALL_STRING_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-+_';
|
||||
|
||||
/**
|
||||
* @param $length
|
||||
* @return false|string
|
||||
*/
|
||||
public static function rand($length = 16)
|
||||
{
|
||||
return substr(str_shuffle(self::ALL_STRING_CHARS), 0, $length > self::MAX_RAND_LENGTH ? self::DEFAULT_RAND_LENGTH : ($length < self::MIN_RAND_LENGTH ? self::DEFAULT_RAND_LENGTH : $length));
|
||||
}
|
||||
}
|
||||
95
extend/Yzh/Utils/Rsa.php
Normal file
95
extend/Yzh/Utils/Rsa.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace Yzh\Utils;
|
||||
|
||||
use Yzh\Exception\ConfigException;
|
||||
use Yzh\Exception\ExceptionCode;
|
||||
|
||||
class Rsa
|
||||
{
|
||||
private $private_key;
|
||||
private $public_key;
|
||||
private $mode = "SHA256";
|
||||
|
||||
public function __construct($privateKey = "", $publicKey = "")
|
||||
{
|
||||
if (!extension_loaded("openssl")) {
|
||||
throw new ConfigException("缺少 openssl 扩展", ExceptionCode::CONFIG_ERROR_LACK_PACKAGE);
|
||||
}
|
||||
|
||||
$this->private_key = openssl_pkey_get_private($privateKey);
|
||||
if (!$this->private_key) {
|
||||
throw new ConfigException("私钥解析失败", ExceptionCode::CONFIG_ERROR_RSA_KEY_INVALID);
|
||||
}
|
||||
|
||||
$this->public_key = openssl_pkey_get_public($publicKey);
|
||||
if (!$this->public_key) {
|
||||
throw new ConfigException("公钥解析失败", ExceptionCode::CONFIG_ERROR_RSA_KEY_INVALID);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 私钥加密
|
||||
* @param $value
|
||||
* @return string
|
||||
*/
|
||||
public function encrypt($value)
|
||||
{
|
||||
$encrypted = "";
|
||||
if (!openssl_private_encrypt($value, $encrypted, $this->private_key)) {
|
||||
throw new ConfigException("openssl 加密失败", ExceptionCode::PRIVATE_KEY_ENCRYPT_FAIL);
|
||||
}
|
||||
return $encrypted;
|
||||
}
|
||||
|
||||
/**
|
||||
* 私钥解密
|
||||
* @param $encrypted
|
||||
* @return string
|
||||
*/
|
||||
public function privateKeyDecrypt($encrypted)
|
||||
{
|
||||
$decrypted = "";
|
||||
if (!openssl_private_decrypt($encrypted, $decrypted, $this->private_key)) {
|
||||
throw new ConfigException("openssl 解密失败", ExceptionCode::PRIVATE_KEY_DECRYPT_FAIL);
|
||||
}
|
||||
return $decrypted;
|
||||
}
|
||||
|
||||
/**
|
||||
* 公钥解密
|
||||
* @param $encrypted
|
||||
* @return string
|
||||
*/
|
||||
public function decrypt($encrypted)
|
||||
{
|
||||
$decrypted = "";
|
||||
if (!openssl_public_decrypt($encrypted, $decrypted, $this->public_key)) {
|
||||
throw new ConfigException("openssl 解密失败", ExceptionCode::PUBLIC_KEY_DECRYPT_FAIL);
|
||||
}
|
||||
return $decrypted;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据签名
|
||||
* @param $data
|
||||
* @return string
|
||||
*/
|
||||
public function sign($data)
|
||||
{
|
||||
openssl_sign($data, $sign, $this->private_key, $this->mode);
|
||||
return base64_encode($sign);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证数据签名
|
||||
* @param string $data
|
||||
* @param string $sign
|
||||
* @return bool
|
||||
*/
|
||||
public function verify($data, $sign)
|
||||
{
|
||||
$result = openssl_verify($data, base64_decode($sign), $this->public_key, $this->mode);
|
||||
return $result == 1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user