add clear
This commit is contained in:
+38
-25
@@ -4,7 +4,7 @@
|
||||
namespace wchat;
|
||||
|
||||
|
||||
class Help extends Base
|
||||
class Help extends Miniprogarampage
|
||||
{
|
||||
public static $OK = 0;
|
||||
public static $IllegalAesKey = -41001;
|
||||
@@ -12,52 +12,43 @@ class Help extends Base
|
||||
public static $IllegalBuffer = -41003;
|
||||
public static $DecodeBase64Error = -41004;
|
||||
|
||||
public static function d($code)
|
||||
{
|
||||
$messages = [
|
||||
static::$OK => '',
|
||||
static::$IllegalAesKey => '',
|
||||
static::$IllegalIv => '',
|
||||
static::$IllegalBuffer => '',
|
||||
static::$DecodeBase64Error => '',
|
||||
];
|
||||
return $messages[$code] ?? static::$DecodeBase64Error;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $encryptedData
|
||||
* @param $iv
|
||||
* @param $sessionKey
|
||||
* @param $data
|
||||
* @param null $appId
|
||||
* @return int
|
||||
* @throws
|
||||
*
|
||||
* * <li>-41001: encodingAesKey 非法</li>
|
||||
* <li>-41003: aes 解密失败</li>
|
||||
* <li>-41004: 解密后得到的buffer非法</li>
|
||||
* <li>-41005: base64加密失败</li>
|
||||
* <li>-41016: base64解密失败</li>
|
||||
*/
|
||||
public static function decode($encryptedData, $iv, $sessionKey, &$data, $appId = null)
|
||||
public static function decode($encryptedData, $iv, $sessionKey)
|
||||
{
|
||||
$config = Wx::getMiniProGaRamPage()->getConfig();
|
||||
if (strlen($sessionKey) != 24) {
|
||||
return self::$IllegalAesKey;
|
||||
throw new \Exception('encodingAesKey 非法', self::$IllegalAesKey);
|
||||
}
|
||||
|
||||
flush();
|
||||
$aesKey = base64_decode($sessionKey);
|
||||
if (strlen($iv) != 24) {
|
||||
return self::$IllegalIv;
|
||||
throw new \Exception('base64解密失败', self::$IllegalIv);
|
||||
}
|
||||
|
||||
$aesIV = base64_decode($iv);
|
||||
$aesCipher = base64_decode($encryptedData);
|
||||
|
||||
$result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, OPENSSL_RAW_DATA, $aesIV);
|
||||
if ($result === false) {
|
||||
return self::$IllegalBuffer;
|
||||
throw new \Exception('aes 解密失败', self::$IllegalBuffer);
|
||||
}
|
||||
|
||||
$dataObj = json_decode($result);
|
||||
if ($dataObj->watermark->appid != $appId) {
|
||||
return self::$IllegalBuffer;
|
||||
if ($dataObj->watermark->appid != $config->getAppid()) {
|
||||
throw new \Exception('aes 解密失败', self::$IllegalBuffer);
|
||||
}
|
||||
$data = $dataObj;
|
||||
return self::$OK;
|
||||
return $dataObj;
|
||||
}
|
||||
|
||||
|
||||
@@ -97,6 +88,28 @@ class Help extends Base
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $length
|
||||
* @return string
|
||||
*
|
||||
* 随机字符串
|
||||
*/
|
||||
public static function random($length = 20)
|
||||
{
|
||||
$res = [];
|
||||
$str = 'abcdefghijklmnopqrstuvwxyz';
|
||||
$str .= strtoupper($str) . '1234567890';
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$rand = substr($str, rand(0, strlen($str) - 2), 1);
|
||||
if (empty($rand)) {
|
||||
$rand = substr($str, strlen($str) - 3, 1);
|
||||
}
|
||||
array_push($res, $rand);
|
||||
}
|
||||
|
||||
return implode($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $array
|
||||
* @param $key
|
||||
|
||||
Reference in New Issue
Block a user