Files
hyperf_service/app/Lib/Crypto/CryptoFactory.php
2024-10-27 00:34:45 +08:00

57 lines
1.4 KiB
PHP

<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Lib\Crypto;
use Exception;
/**
* 加密工厂
*/
class CryptoFactory
{
/**
* 加密类主体
* @var CryptoInterface
*/
protected CryptoInterface $cryptoInterface;
/**
* 加密工厂
* @param string $type
* @param string $dataStr
* @param string $key
* @return ApiCrypto|CryptoInterface|JwtCrypto
* @throws Exception
*/
public function cryptoClass(string $type, string $dataStr, string $key = ''): JwtCrypto|CryptoInterface|ApiCrypto
{
switch ($type) {
case 'api':
$apiCrypto = new ApiCrypto();
$this->cryptoInterface = $apiCrypto;
break;
case 'jwt':
$jwtCrypto = new JwtCrypto();
$this->cryptoInterface = $jwtCrypto;
break;
case 'admin-password':
$adminCrypto = new AdminPasswordCrypto();
$this->cryptoInterface = $adminCrypto;
$this->cryptoInterface->salt = $key;
break;
default:
throw new Exception('The encryption algorithm does not exist');
}
$this->cryptoInterface->data = $dataStr;
return $this->cryptoInterface;
}
}