first commit
This commit is contained in:
@@ -0,0 +1 @@
|
||||
f7bd8e9f-378c-4ca6-83a3-d6fae4656b5b
|
||||
@@ -0,0 +1,267 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
*/
|
||||
|
||||
namespace app\common\controller;
|
||||
|
||||
class Encryption
|
||||
{
|
||||
|
||||
private $publicKey;
|
||||
|
||||
private $privateKey;
|
||||
|
||||
private $keyLength;
|
||||
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
$this->publicKey = config("encset.rsa")['publicKey'];
|
||||
$this->privateKey = config("encset.rsa")['privateKey'];
|
||||
|
||||
// 公钥
|
||||
$publicId = openssl_pkey_get_public($this->publicKey);
|
||||
$this->keyLength = openssl_pkey_get_details($publicId)['bits'];
|
||||
|
||||
// 私钥
|
||||
$privateId = openssl_pkey_get_private($this->privateKey);
|
||||
$this->keyLength = openssl_pkey_get_details($privateId)['bits'];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 生成签名
|
||||
*
|
||||
* @param string 签名材料
|
||||
* @param string 签名编码(base64/hex/bin)
|
||||
* @return 签名值
|
||||
*/
|
||||
public function sign($data, $code = 'base64')
|
||||
{
|
||||
$ret = false;
|
||||
$privateId = openssl_get_privatekey($this->privateKey);
|
||||
if (openssl_sign($data, $ret, $privateId)) {
|
||||
$ret = $this->encodeSign($ret, $code);
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 验证签名
|
||||
*
|
||||
* @param string 签名材料
|
||||
* @param string 签名值
|
||||
* @param string 签名编码(base64/hex/bin)
|
||||
* @return bool
|
||||
*/
|
||||
public function verify($data, $sign, $code = 'base64')
|
||||
{
|
||||
$ret = false;
|
||||
$sign = $this->decodeSign($sign, $code);
|
||||
if ($sign !== false) {
|
||||
$publicId = openssl_get_publickey($this->publicKey);
|
||||
switch (openssl_verify($data, $sign, $publicId)) {
|
||||
case 1:
|
||||
$ret = true;
|
||||
break;
|
||||
case 0:
|
||||
case -1:
|
||||
default:
|
||||
$ret = false;
|
||||
}
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 加密
|
||||
*
|
||||
* @param string 明文
|
||||
* @param string 密文编码(base64/hex/bin)
|
||||
* @param int 填充方式(php目前仅支持OPENSSL_PKCS1_PADDING)
|
||||
* @return string 密文
|
||||
*/
|
||||
public function encrypt($data, $code = 'base64', $padding = OPENSSL_PKCS1_PADDING)
|
||||
{
|
||||
$ret = false;
|
||||
if (!$this->checkPadding($padding, 'en')) $this->errorInfo('padding error');
|
||||
if (openssl_public_encrypt($data, $result, $this->publicKey, $padding)) {
|
||||
$ret = $this->encodeSign($result, $code);
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 解密
|
||||
*
|
||||
* @param string 密文
|
||||
* @param string 密文编码(base64/hex/bin)
|
||||
* @param int 填充方式(OPENSSL_PKCS1_PADDING / OPENSSL_NO_PADDING)
|
||||
* @param bool 是否翻转明文(When passing Microsoft CryptoAPI-generated RSA cyphertext, revert the bytes in the block)
|
||||
* @return string 明文
|
||||
*/
|
||||
public function decrypt($data, $code = 'base64', $padding = OPENSSL_PKCS1_PADDING, $rev = false)
|
||||
{
|
||||
$ret = false;
|
||||
$data = $this->decodeSign($data, $code);
|
||||
if (!$this->checkPadding($padding, 'de')) $this->errorInfo('padding error');
|
||||
if ($data !== false) {
|
||||
if (openssl_private_decrypt($data, $result, $this->privateKey, $padding)) {
|
||||
$ret = $rev ? rtrim(strrev($result), "\0") : '' . $result;
|
||||
}
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取key信息
|
||||
* @return array
|
||||
*/
|
||||
public function getKeyInfo()
|
||||
{
|
||||
$result = [
|
||||
"publicInfo" => [
|
||||
"publicKey" => $this->publicKey,
|
||||
"keyLength" => $this->keyLength,
|
||||
],
|
||||
"privateInfo" => [
|
||||
"privateKey" => $this->privateKey,
|
||||
"keyLength" => $this->keyLength,
|
||||
],
|
||||
];
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/***************************************************** 私有方法 ****************************************************/
|
||||
|
||||
/**
|
||||
* 检测填充类型
|
||||
* 加密只支持PKCS1_PADDING
|
||||
* 解密支持PKCS1_PADDING和NO_PADDING
|
||||
*
|
||||
* @param int 填充模式
|
||||
* @param string 加密en/解密de
|
||||
* @return bool
|
||||
*/
|
||||
private function checkPadding($padding, $type)
|
||||
{
|
||||
if ($type == 'en') {
|
||||
switch ($padding) {
|
||||
case OPENSSL_PKCS1_PADDING:
|
||||
$ret = true;
|
||||
break;
|
||||
default:
|
||||
$ret = false;
|
||||
}
|
||||
} else {
|
||||
switch ($padding) {
|
||||
case OPENSSL_PKCS1_PADDING:
|
||||
case OPENSSL_NO_PADDING:
|
||||
$ret = true;
|
||||
break;
|
||||
default:
|
||||
$ret = false;
|
||||
}
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 加码
|
||||
* @param $data
|
||||
* @param $code
|
||||
* @return bool|string
|
||||
*/
|
||||
private function encodeSign($data, $code)
|
||||
{
|
||||
switch (strtolower($code)) {
|
||||
case 'base64':
|
||||
$data = base64_encode('' . $data);
|
||||
break;
|
||||
case 'hex':
|
||||
$data = bin2hex($data);
|
||||
break;
|
||||
case 'bin':
|
||||
default:
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 解码
|
||||
* @param $sign
|
||||
* @param $code
|
||||
* @return bool|string
|
||||
*/
|
||||
private function decodeSign($sign, $code)
|
||||
{
|
||||
$data = false;
|
||||
switch (strtolower($code)) {
|
||||
case 'base64':
|
||||
$data = base64_decode($sign);
|
||||
break;
|
||||
case 'hex':
|
||||
$data = $this->hex2Bin($sign);
|
||||
break;
|
||||
case 'bin':
|
||||
default:
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 自定义错误处理
|
||||
* @param $msg
|
||||
*/
|
||||
private function errorInfo($msg)
|
||||
{
|
||||
die('RSA Error:' . $msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* hex2Bin
|
||||
* @param bool $hex
|
||||
* @return bool|string
|
||||
*/
|
||||
private function hex2Bin($hex = false)
|
||||
{
|
||||
$ret = $hex !== false && preg_match('/^[0-9a-fA-F]+$/i', $hex) ? pack("H*", $hex) : false;
|
||||
return $ret;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* sha256 加密
|
||||
* @param $data
|
||||
* @param bool $rawOutput
|
||||
* @return bool|string
|
||||
*/
|
||||
public function sha256($data, $rawOutput = false)
|
||||
{
|
||||
if (!is_scalar($data)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = (string)$data;
|
||||
$rawOutput = !!$rawOutput;
|
||||
|
||||
return hash('sha256', $data, $rawOutput);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\controller;
|
||||
|
||||
/**
|
||||
* 签名助手 2017/11/19
|
||||
*
|
||||
* Class SignatureHelper
|
||||
*/
|
||||
class SignatureHelper {
|
||||
|
||||
/**
|
||||
* 生成签名并发起请求
|
||||
*
|
||||
* @param $accessKeyId string AccessKeyId (https://ak-console.aliyun.com/)
|
||||
* @param $accessKeySecret string AccessKeySecret
|
||||
* @param $domain string API接口所在域名
|
||||
* @param $params array API具体参数
|
||||
* @param $security boolean 使用https
|
||||
* @param $method boolean 使用GET或POST方法请求,VPC仅支持POST
|
||||
* @return bool|\stdClass 返回API接口调用结果,当发生错误时返回false
|
||||
*/
|
||||
public function request($accessKeyId, $accessKeySecret, $domain, $params, $security=false, $method='POST') {
|
||||
$apiParams = array_merge(array (
|
||||
"SignatureMethod" => "HMAC-SHA1",
|
||||
"SignatureNonce" => uniqid(mt_rand(0,0xffff), true),
|
||||
"SignatureVersion" => "1.0",
|
||||
"AccessKeyId" => $accessKeyId,
|
||||
"Timestamp" => gmdate("Y-m-d\TH:i:s\Z"),
|
||||
"Format" => "JSON",
|
||||
), $params);
|
||||
ksort($apiParams);
|
||||
|
||||
$sortedQueryStringTmp = "";
|
||||
foreach ($apiParams as $key => $value) {
|
||||
$sortedQueryStringTmp .= "&" . $this->encode($key) . "=" . $this->encode($value);
|
||||
}
|
||||
|
||||
$stringToSign = "${method}&%2F&" . $this->encode(substr($sortedQueryStringTmp, 1));
|
||||
|
||||
$sign = base64_encode(hash_hmac("sha1", $stringToSign, $accessKeySecret . "&",true));
|
||||
|
||||
$signature = $this->encode($sign);
|
||||
|
||||
$url = ($security ? 'https' : 'http')."://{$domain}/";
|
||||
|
||||
try {
|
||||
$content = $this->fetchContent($url, $method, "Signature={$signature}{$sortedQueryStringTmp}");
|
||||
return json_decode($content);
|
||||
} catch( \Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private function encode($str)
|
||||
{
|
||||
$res = urlencode($str);
|
||||
$res = preg_replace("/\+/", "%20", $res);
|
||||
$res = preg_replace("/\*/", "%2A", $res);
|
||||
$res = preg_replace("/%7E/", "~", $res);
|
||||
return $res;
|
||||
}
|
||||
|
||||
private function fetchContent($url, $method, $body) {
|
||||
$ch = curl_init();
|
||||
|
||||
if($method == 'POST') {
|
||||
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
|
||||
} else {
|
||||
$url .= '?'.$body;
|
||||
}
|
||||
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
|
||||
"x-sdk-client" => "php/2.0.0"
|
||||
));
|
||||
|
||||
if(substr($url, 0,5) == 'https') {
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||||
}
|
||||
|
||||
$rtn = curl_exec($ch);
|
||||
|
||||
if($rtn === false) {
|
||||
// 大多由设置等原因引起,一般无法保障后续逻辑正常执行,
|
||||
// 所以这里触发的是E_USER_ERROR,会终止脚本执行,无法被try...catch捕获,需要用户排查环境、网络等故障
|
||||
trigger_error("[CURL_" . curl_errno($ch) . "]: " . curl_error($ch), E_USER_ERROR);
|
||||
}
|
||||
curl_close($ch);
|
||||
|
||||
return $rtn;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user