mirror of https://gitee.com/topnuomi/aes-rsa
更新RSA部分注释
This commit is contained in:
parent
9e5b3b2265
commit
219c179a83
|
@ -8,7 +8,7 @@ interface RSA
|
|||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -38,33 +38,35 @@ class RSA implements \encrypt\RSA
|
|||
{
|
||||
$this->config = $config;
|
||||
if (!isset($this->config['public_key'], $this->config['private_key'])) {
|
||||
throw new \Exception('公钥私钥参数错误');
|
||||
throw new \Exception('密钥参数错误');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取公钥
|
||||
* @return string
|
||||
* @return null|resource
|
||||
*/
|
||||
public function publicKey()
|
||||
{
|
||||
if (!is_file($this->config['public_key'])) {
|
||||
return '';
|
||||
return null;
|
||||
}
|
||||
$content = file_get_contents($this->config['public_key']);
|
||||
|
||||
return openssl_pkey_get_public($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取私钥
|
||||
* @return string
|
||||
* @return null|resource
|
||||
*/
|
||||
public function privateKey()
|
||||
{
|
||||
if (!is_file($this->config['private_key'])) {
|
||||
return '';
|
||||
return null;
|
||||
}
|
||||
$content = file_get_contents($this->config['private_key']);
|
||||
|
||||
return openssl_pkey_get_private($content);
|
||||
}
|
||||
|
||||
|
@ -72,9 +74,9 @@ class RSA implements \encrypt\RSA
|
|||
* 加密数据
|
||||
* @param string $data
|
||||
* @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) {
|
||||
$func = 'openssl_private_encrypt';
|
||||
|
@ -86,8 +88,8 @@ class RSA implements \encrypt\RSA
|
|||
|
||||
$string = '';
|
||||
foreach (str_split($data, 117) as $chunk) {
|
||||
/** @var string $encrypted */
|
||||
$string .= ($func($chunk, $encrypted, $key) ? base64_encode($encrypted) : null);
|
||||
/** @var string $encryptData */
|
||||
$string .= ($func($chunk, $encryptData, $key) ? base64_encode($encryptData) : null);
|
||||
}
|
||||
|
||||
return $string;
|
||||
|
@ -97,9 +99,9 @@ class RSA implements \encrypt\RSA
|
|||
* 解密数据
|
||||
* @param string $data
|
||||
* @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) {
|
||||
$func = 'openssl_private_decrypt';
|
||||
|
|
Loading…
Reference in New Issue