jh-admin/app/mp/model/HwcloudOrder.php

159 lines
5.2 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\mp\model;
use util\Random;
class HwcloudOrder extends CloudOrder
{
public $data;
public $urls = 'https://cloud.sopvip.com';
protected $mode = MCRYPT_MODE_CBC;//采用cbc加密模式
protected $key = 'bbc27f93-b492-462f-8f5b-4e6c2778cbec'; //密钥
protected $iv; //cbc模式加密向量如为空将采用密钥代替
public function OutDataInfo()
{
$this->data = input();
if ($this->signVerify()) {
switch ($this->data['activity']) {
case 'newInstance': //新建
$info = $this->createInstance();
break;
case 'refreshInstance': //续费
$info = $this->refreshInstance();
break;
case 'expireInstance': //过期
$info = $this->expireInstance();
break;
case 'releaseInstance': //过期
$info = $this->releaseInstance();
break;
}
return $info;
}
return;
}
public function createInstance()
{
$arr = [
'resultCode' => '000000',
'resultMsg' => 'success',
'instanceId' => '03pf80c2bae96vc49b80b917bea776d7',
'encryptType' => '2',
'appInfo' => [
'frontEndUrl' => $this->urls,
'adminUrl' => $this->urls,
'userName' => $this->encrypt('18982255122'),
'password' => $this->encrypt('110120')
],
];
$BodyToken = base64_encode(hash_hmac('sha256', json_encode($arr), $this->key, true));
header(sprintf('Body-Sign: sign_type="HMAC-SHA256",signature="%s"', $BodyToken));
return $arr;
}
/***
* 续费,转正
* @return array
*/
public function refreshInstance()
{
$arr = [
'resultCode' => '000000',
'resultMsg' => 'success'
];
$BodyToken = base64_encode(hash_hmac('sha256', json_encode($arr), $this->key, true));
header(sprintf('Body-Sign: sign_type="HMAC-SHA256",signature="%s"', $BodyToken));
return $arr;
}
/***
* 过期
* @return array
*/
public function expireInstance()
{
$arr = [
'resultCode' => '000000',
'resultMsg' => 'success'
];
$BodyToken = base64_encode(hash_hmac('sha256', json_encode($arr), $this->key, true));
header(sprintf('Body-Sign: sign_type="HMAC-SHA256",signature="%s"', $BodyToken));
return $arr;
}
/***
* 应用释放
* @return array
*/
public function releaseInstance()
{
$arr = [
'resultCode' => '000000',
'resultMsg' => 'success'
];
$BodyToken = base64_encode(hash_hmac('sha256', json_encode($arr), $this->key, true));
header(sprintf('Body-Sign: sign_type="HMAC-SHA256",signature="%s"', $BodyToken));
return $arr;
}
public function OutFormat()
{
return 'json';
}
/**
* 验证签名
* @return bool
*/
private function signVerify()
{
return true;
file_put_contents('hw.txt', json_encode(input()));
$authToken = input('authToken', '', null);
$data = request()->param();
unset($data['authToken'], $data['type']);
ksort($data);
//生成authToken值base64_encode(HMAC_SHA256(Key+timeStamp, p1=1&p3=3&p2=2&timeStamp=201706211855321))
$signToken = base64_encode(hash_hmac('sha256', urldecode(http_build_query($data)), $this->key . $data['timeStamp'], true));
if ($authToken == $signToken) {
return true;
} else {
return false;
}
}
//加密
private function encrypt($encrypt, $key = '')
{
$blockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$paddedData = $this->_pkcs5Pad($encrypt, $blockSize);
$iv = Random::alnum(16);
$key2 = substr(openssl_digest(openssl_digest($this->key, 'sha1', true), 'sha1', true), 0, 16);
$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key2, $paddedData, MCRYPT_MODE_CBC, $iv);
return $iv . base64_encode($encrypted);
}
//解密
private function decrypt($data)
{
$decoded = substr($data, 16);
$iv = substr($data, 0, 16);
$decoded = base64_decode($decoded);
$blockSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$key2 = substr(openssl_digest(openssl_digest($this->key, 'sha1', true), 'sha1', true), 0, 16);
$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key2, $decoded, MCRYPT_MODE_CBC, $iv);
return $this->_pkcs5Unpad($decrypted);
}
//PKCS5Padding 补码方式
private function _pkcs5Pad($text, $blockSize)
{
$pad = $blockSize - (strlen($text) % $blockSize);
return $text . str_repeat(chr($pad), $pad);
}
private function _pkcs5Unpad($text)
{
$end = substr($text, -1);
$last = ord($end);
$len = strlen($text) - $last;
if (substr($text, $len) == str_repeat($end, $last)) {
return substr($text, 0, $len);
}
return false;
}
}