feat: admin login
This commit is contained in:
76
app/Lib/Crypto/ApiCrypto.php
Normal file
76
app/Lib/Crypto/ApiCrypto.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
/**
|
||||
* This lib file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Lib\Crypto;
|
||||
|
||||
use Exception;
|
||||
use function Hyperf\Config\config;
|
||||
|
||||
/**
|
||||
* 接口加密类
|
||||
*/
|
||||
class ApiCrypto implements CryptoInterface
|
||||
{
|
||||
/**
|
||||
* 加密数据
|
||||
* @var string
|
||||
*/
|
||||
public string $data = '';
|
||||
|
||||
/**
|
||||
* 加密key
|
||||
* @var string
|
||||
*/
|
||||
private string $key;
|
||||
|
||||
/**
|
||||
* 构造方法 配置写入
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->key = config('system.api_return_key');
|
||||
}
|
||||
|
||||
/**
|
||||
* api加密接口
|
||||
* @return string
|
||||
*/
|
||||
public function encrypt(): string
|
||||
{
|
||||
try {
|
||||
//设置偏移量
|
||||
$iv = substr(md5($this->data), 0, 16);
|
||||
//使用 openssl 加密数据
|
||||
$encrypted = openssl_encrypt($this->data,'AES-128-CBC',$this->key,OPENSSL_RAW_DATA,$iv);
|
||||
return $iv.'|'.base64_encode($encrypted);
|
||||
} catch (Exception) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* api解密接口
|
||||
* @return string
|
||||
*/
|
||||
public function decrypt(): string
|
||||
{
|
||||
try {
|
||||
$array = explode('|',$this->data);
|
||||
//获取偏移量
|
||||
$iv = $array[0];
|
||||
//获取加密数据
|
||||
$encrypted = base64_decode($array[1]);
|
||||
//使用 openssl 解密数据 并返回
|
||||
return openssl_decrypt($encrypted, 'AES-128-CBC',$this->key,OPENSSL_RAW_DATA,$iv);
|
||||
} catch (Exception) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user