57 lines
1.4 KiB
PHP
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;
|
|
}
|
|
} |