-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SensitiveInfoCrypto 这个类过度设计了,看起来简单,其实并不简单 #25
Comments
另外传进去的证书也不一定要是 resource 啊,放缓存里也是可以的吧 |
@ifreesec 有什么建议吗,另外也欢迎PR。 |
传入的$publicCert参数类型为resource其实本意是指使用 |
这个理解了,感谢 |
我的想法很简单,就是按正常的方法去使用加密和解密,正常的拆分成两个类 //加密
class SensitiveInfoEncrypt
{
/**
* string 的话可以 PemUtil::loadCertificateFromString 处理下在传入
*
* @var resource|null $publicCert The public certificate
*/
private $publicCert;
/**
* SensitiveInfoEncrypt constructor.
* @param null $publicCert 微信支付平台证书
*/
public function __construct($publicCert)
{
$this->publicCert = $publicCert;
}
/**
* @param $str
* @return string
*/
public function encrypt($str)
{
if (!is_resource($this->publicCert)) {
throw new \InvalidArgumentException('The publicCert must be resource.');
}
openssl_public_encrypt($str, $encrypted,
$this->publicCert, \OPENSSL_PKCS1_OAEP_PADDING);
return \base64_encode($encrypted);
}
}
//解密
class SensitiveInfoDecrypt
{
/**
* @var resource|null $privateKey The private key
*/
private $privateKey;
/**
* SensitiveInfoDecrypt constructor.
* @param null $privateKey 微信商户私钥
*/
public function __construct($privateKey = null)
{
$this->privateKey = $privateKey;
}
/**
* 解密
* @param $str
* @return string
*/
public function decrypt($str)
{
if (!is_resource($this->privateKey)) {
throw new \InvalidArgumentException('The privateKey must be resource.');
}
openssl_private_decrypt(\base64_decode($str), $decrypted,
$this->privateKey, \OPENSSL_PKCS1_OAEP_PADDING);
return $decrypted;
}
} |
刚看到提醒,抱歉。 楼主批评得对,这用法上基本都是单一场景,要么加密,要么解密,俩揉在一起是有点过度设计了,而且容易造成干扰。。。 |
新包设计上,加/解密用法是:
环境要求 |
No description provided.
The text was updated successfully, but these errors were encountered: