初始化代码
This commit is contained in:
10
application/common/yun/util/Demo.php
Normal file
10
application/common/yun/util/Demo.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace app\common\yun\util;
|
||||
|
||||
|
||||
class Demo
|
||||
{
|
||||
|
||||
}
|
||||
59
application/common/yun/util/DesUtil.php
Normal file
59
application/common/yun/util/DesUtil.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
namespace app\common\yun\util;
|
||||
class DesUtil
|
||||
{
|
||||
/**
|
||||
* 密钥向量
|
||||
* @var string
|
||||
*/
|
||||
private $des3key;
|
||||
|
||||
/**
|
||||
* 混淆向量
|
||||
* @var string|null
|
||||
*/
|
||||
private $iv;
|
||||
|
||||
/**
|
||||
* 构造,传递⼆个已经进⾏base64_encode的KEY与IV
|
||||
*
|
||||
* @param string $des3key
|
||||
* @param string $iv
|
||||
*/
|
||||
function __construct($des3key, $iv = null)
|
||||
{
|
||||
$this->des3key = $des3key;
|
||||
$this->iv = $iv;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密
|
||||
* @param <type> $value
|
||||
* @return <type>
|
||||
*/
|
||||
public function encrypt($value)
|
||||
{
|
||||
$iv = substr($this->des3key, 0, 8);
|
||||
$ret = openssl_encrypt($value, 'DES-EDE3-CBC', $this->des3key, 0, $iv);
|
||||
if (false === $ret) {
|
||||
return openssl_error_string();
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密
|
||||
* @param <type> $value
|
||||
* @return <type>
|
||||
*/
|
||||
public function decrypt($value)
|
||||
{
|
||||
$iv = substr($this->des3key, 0, 8);
|
||||
$ret = openssl_decrypt($value, 'DES-EDE3-CBC', $this->des3key, 0, $iv);
|
||||
if (false === $ret) {
|
||||
return openssl_error_string();
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
126
application/common/yun/util/RsaUtil.php
Normal file
126
application/common/yun/util/RsaUtil.php
Normal file
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
namespace app\common\yun\util;
|
||||
use app\common\yun\Config;
|
||||
class RsaUtil
|
||||
{
|
||||
/**
|
||||
* 相关配置
|
||||
* @var Config
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* 云账户公钥
|
||||
* @var
|
||||
*/
|
||||
protected $public_key;
|
||||
|
||||
|
||||
/**
|
||||
* 商户私钥
|
||||
* @var
|
||||
*/
|
||||
protected $private_key;
|
||||
|
||||
/**
|
||||
* 初始化配置
|
||||
* RsaService constructor.
|
||||
* @param bool $type 默认私钥加密
|
||||
*/
|
||||
public function __construct(Config $config,$type = true)
|
||||
{
|
||||
$this->config = $config;
|
||||
if ($type) {
|
||||
$this->private_key = $this->getPrivateKey(); #商户私钥
|
||||
$this->public_key = $this->getPublicKey(); #云账户公钥
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 配置私钥
|
||||
* openssl_pkey_get_private这个函数可用来判断私钥是否是可用的,可用,返回资源
|
||||
* @return bool|resource
|
||||
*/
|
||||
private function getPrivateKey()
|
||||
{
|
||||
|
||||
$privateKey = openssl_get_privatekey($this->config->private_key);
|
||||
if(!$privateKey){
|
||||
die('私钥不可用');
|
||||
}
|
||||
return $privateKey;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 配置公钥
|
||||
* openssl_pkey_get_public这个函数可用来判断私钥是否是可用的,可用,返回资源
|
||||
* @return resource
|
||||
*/
|
||||
public function getPublicKey()
|
||||
{
|
||||
|
||||
$publicKey = openssl_pkey_get_public($this->config->public_key);
|
||||
if(!$publicKey){
|
||||
die('公钥不可用');
|
||||
}
|
||||
|
||||
return $publicKey ;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 签名算法
|
||||
* @access public
|
||||
* @param $data
|
||||
* @return string
|
||||
*/
|
||||
public function sign($data){
|
||||
$res=openssl_get_privatekey($this->getPrivateKey());
|
||||
if($res)
|
||||
{
|
||||
openssl_sign($data, $sign,$res,"SHA256");
|
||||
openssl_free_key($res);
|
||||
}else {
|
||||
exit("私钥格式有误");
|
||||
}
|
||||
$sign = base64_encode($sign);
|
||||
return $sign;
|
||||
|
||||
}
|
||||
/**
|
||||
* 验签
|
||||
* @access public
|
||||
* @param $data
|
||||
* @return
|
||||
*/
|
||||
public function verify($response){
|
||||
$signData = "data=".$response['data']."&mess=".$response['mess']."×tamp=".$response['timestamp']."&key=".$this->config->app_key;
|
||||
// echo $signData;die;
|
||||
$result = (bool)openssl_verify( $signData, base64_decode($response['sign']), $this->public_key,"SHA256");
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 私钥解密
|
||||
* @param $data
|
||||
* @param bool $unserialize
|
||||
* @return mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function privateDecrypt($data, $unserialize = false)
|
||||
{
|
||||
openssl_private_decrypt(base64_decode($data),$decrypted, $this->private_key);
|
||||
|
||||
if ($decrypted === false) {
|
||||
throw new \Exception('Could not decrypt the data.');
|
||||
}
|
||||
|
||||
return $unserialize ? unserialize($decrypted) : $decrypted;
|
||||
}
|
||||
|
||||
}
|
||||
17
application/common/yun/util/StringUtil.php
Normal file
17
application/common/yun/util/StringUtil.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
namespace app\common\yun\util;
|
||||
class StringUtil
|
||||
{
|
||||
public static function round($len = 6)
|
||||
{
|
||||
$str = 'a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9';
|
||||
$arr = explode(' ', $str);
|
||||
$rand_keys = array_rand($arr, $len);
|
||||
shuffle($rand_keys);
|
||||
$code = '';
|
||||
foreach ($rand_keys as $index=>$key){
|
||||
$code .= $arr[$key];
|
||||
}
|
||||
return $code;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user