更新RSA部分注释

This commit is contained in:
TOP糯米 2020-10-21 18:36:02 +08:00
parent 9e5b3b2265
commit 219c179a83
2 changed files with 15 additions and 13 deletions

View File

@ -8,7 +8,7 @@ interface RSA
public function privateKey(); public function privateKey();
public function encrypt(string $data): ?string; public function encrypt(string $data): string;
public function decrypt(string $data): ?string; public function decrypt(string $data): string;
} }

View File

@ -38,33 +38,35 @@ class RSA implements \encrypt\RSA
{ {
$this->config = $config; $this->config = $config;
if (!isset($this->config['public_key'], $this->config['private_key'])) { if (!isset($this->config['public_key'], $this->config['private_key'])) {
throw new \Exception('公钥私钥参数错误'); throw new \Exception('钥参数错误');
} }
} }
/** /**
* 获取公钥 * 获取公钥
* @return string * @return null|resource
*/ */
public function publicKey() public function publicKey()
{ {
if (!is_file($this->config['public_key'])) { if (!is_file($this->config['public_key'])) {
return ''; return null;
} }
$content = file_get_contents($this->config['public_key']); $content = file_get_contents($this->config['public_key']);
return openssl_pkey_get_public($content); return openssl_pkey_get_public($content);
} }
/** /**
* 获取私钥 * 获取私钥
* @return string * @return null|resource
*/ */
public function privateKey() public function privateKey()
{ {
if (!is_file($this->config['private_key'])) { if (!is_file($this->config['private_key'])) {
return ''; return null;
} }
$content = file_get_contents($this->config['private_key']); $content = file_get_contents($this->config['private_key']);
return openssl_pkey_get_private($content); return openssl_pkey_get_private($content);
} }
@ -72,9 +74,9 @@ class RSA implements \encrypt\RSA
* 加密数据 * 加密数据
* @param string $data * @param string $data
* @param int $type * @param int $type
* @return null|string * @return string
*/ */
public function encrypt(string $data, $type = self::USE_PRIVATE): ?string public function encrypt(string $data, int $type = self::USE_PRIVATE): string
{ {
if (self::USE_PRIVATE === $type) { if (self::USE_PRIVATE === $type) {
$func = 'openssl_private_encrypt'; $func = 'openssl_private_encrypt';
@ -86,8 +88,8 @@ class RSA implements \encrypt\RSA
$string = ''; $string = '';
foreach (str_split($data, 117) as $chunk) { foreach (str_split($data, 117) as $chunk) {
/** @var string $encrypted */ /** @var string $encryptData */
$string .= ($func($chunk, $encrypted, $key) ? base64_encode($encrypted) : null); $string .= ($func($chunk, $encryptData, $key) ? base64_encode($encryptData) : null);
} }
return $string; return $string;
@ -97,9 +99,9 @@ class RSA implements \encrypt\RSA
* 解密数据 * 解密数据
* @param string $data * @param string $data
* @param int $type * @param int $type
* @return null|string * @return string
*/ */
public function decrypt(string $data, $type = self::USE_PRIVATE): ?string public function decrypt(string $data, int $type = self::USE_PRIVATE): string
{ {
if (self::USE_PRIVATE === $type) { if (self::USE_PRIVATE === $type) {
$func = 'openssl_private_decrypt'; $func = 'openssl_private_decrypt';