登录加密

This commit is contained in:
zhang zhuo 2026-01-10 16:15:06 +08:00
parent 8ddd29992c
commit 505a892352
3 changed files with 34 additions and 1 deletions

View File

@ -49,6 +49,10 @@ class Login extends Base
$param = Param::only(['username', 'password', 'uuid', 'code']); $param = Param::only(['username', 'password', 'uuid', 'code']);
$request = $this->container->get(aRequest::class); $request = $this->container->get(aRequest::class);
$request->scene('login')->validateResolved(); $request->scene('login')->validateResolved();
// 数据解密
$param['username'] = Str::RsaDecrypt($param['username']);
$param['password'] = Str::RsaDecrypt($param['password']);
$param['code'] = Str::RsaDecrypt($param['code']);
// 验证码 // 验证码
$code = $this->cache->get("VER:" . $param['uuid']); $code = $this->cache->get("VER:" . $param['uuid']);
if (!$code) { if (!$code) {

View File

@ -5,6 +5,8 @@
namespace App\Utils; namespace App\Utils;
use App\Exception\ServiceException;
use function Hyperf\Config\config;
use function PHPUnit\Framework\matches; use function PHPUnit\Framework\matches;
class Str class Str
@ -161,4 +163,27 @@ class Str
return [$first, $rest]; return [$first, $rest];
} }
static function RsaEncrypt(string $data): string
{
$publicKey = "-----BEGIN PUBLIC KEY-----\n" . wordwrap(config('app.rsa_public_key'), 64, "\n", true) . "\n-----END PUBLIC KEY-----";
$ok = openssl_public_encrypt($data, $encrypted, $publicKey, OPENSSL_PKCS1_PADDING);
if (!$ok) {
throw new ServiceException('RSA encrypt failed');
}
return base64_encode($encrypted);
}
static function RsaDecrypt(string $data): string
{
$privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" . wordwrap(config('app.rsa_private_key'), 64, "\n", true) . "\n-----END RSA PRIVATE KEY-----";
$cipher = base64_decode($data);
if ($cipher === false) {
throw new ServiceException('Invalid base64 data');
}
$ok = openssl_private_decrypt($cipher, $plain, $privateKey, OPENSSL_PKCS1_PADDING);
if (!$ok) {
throw new ServiceException('RSA decrypt failed');
}
return $plain;
}
} }

View File

@ -7,5 +7,9 @@ use function Hyperf\Support\env;
*/ */
return [ return [
// 域名 // 域名
'domain' => env('DOMAIN') 'domain' => env('DOMAIN'),
// rsa公钥
'rsa_public_key' => env('RSA_PUBLIC_KEY'),
// rsa私钥
'rsa_private_key' => env('RSA_PRIVATE_KEY'),
]; ];