You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace app\common\lib;
use think\facade\Cache;
class IAuth{
/**
* md5 加密
*/
public static function serPassword($data){
return md5($data,\config('api.password_pre_halt'));
}
/**
* 生成每次請求Sign字符串
*/
public static function setSign($data = []) {
// 按字段排序
\ksort($data);
//拼接字符串數據
$string = \http_build_query($data);
//通過 aes 來加密
$string = (new Aes(\config('api.aes_key')))->aesEn($string);
return $string;
}
/**
* 檢測sign是否正常
*/
public static function checkSignPass($data) {
$str = (new Aes(\config('api.aes_key')))->aesDe($data);
// 判斷解析出來的數據是否為空
if(empty($str)){
return false;
}
// 字符串轉數組
parse_str($str,$arr);
// 判斷是否是數組,數組內的字段是否正確
if(!\is_array($arr) || empty($arr['mg'])){
return false;
}
// 檢測緩存,如果有緩存,説明這個sign已經被使用
if(Cache::get($data)){
return false;
}
return true;
}
}
?>
備註:
'aes_key' =>[
'key' => 'reter4446fdfgdfgdfg', //加密key這個可以隨便定義
'iv' => md5(time(). uniqid(),true), //保證偏移量為16位
'method' => 'AES-128-CBC' //加密方式 # AES-256-CBC等這個可以搭配的形式有很多具體的你可以去了解一下AES加密算法
],