登录加密
This commit is contained in:
parent
8ddd29992c
commit
505a892352
|
|
@ -49,6 +49,10 @@ class Login extends Base
|
|||
$param = Param::only(['username', 'password', 'uuid', 'code']);
|
||||
$request = $this->container->get(aRequest::class);
|
||||
$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']);
|
||||
if (!$code) {
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@
|
|||
|
||||
namespace App\Utils;
|
||||
|
||||
use App\Exception\ServiceException;
|
||||
use function Hyperf\Config\config;
|
||||
use function PHPUnit\Framework\matches;
|
||||
|
||||
class Str
|
||||
|
|
@ -161,4 +163,27 @@ class Str
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -7,5 +7,9 @@ use function Hyperf\Support\env;
|
|||
*/
|
||||
return [
|
||||
// 域名
|
||||
'domain' => env('DOMAIN')
|
||||
'domain' => env('DOMAIN'),
|
||||
// rsa公钥
|
||||
'rsa_public_key' => env('RSA_PUBLIC_KEY'),
|
||||
// rsa私钥
|
||||
'rsa_private_key' => env('RSA_PRIVATE_KEY'),
|
||||
];
|
||||
Loading…
Reference in New Issue