ChannelCa.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace app\common\model;
  3. /**
  4. * 渠道秘钥表
  5. * @author Administrator
  6. */
  7. class ChannelCa extends \think\Model
  8. {
  9. protected $pk = 'id';
  10. protected $table = 'cy_channel_ca';
  11. protected $dateFormat = false; //时间戳格式不自动转换
  12. /**
  13. * 是否有效的私钥
  14. * @param $private_key string
  15. * @return bool
  16. */
  17. protected function _isValidPrivatekey($private_key){
  18. if(empty($private_key)) {
  19. return false;
  20. }
  21. $resource_id = openssl_pkey_get_private($private_key);
  22. if(empty($resource_id)) {
  23. return false;
  24. }
  25. return true;
  26. }
  27. /**
  28. * 是否有效的公钥
  29. * @param $public_key string
  30. * @return bool
  31. */
  32. protected function _isValidPublickey($public_key){
  33. if(empty($public_key)) {
  34. return false;
  35. }
  36. $resource_id = openssl_pkey_get_public($public_key);
  37. if(empty($resource_id)) {
  38. return false;
  39. }
  40. return true;
  41. }
  42. /**
  43. * 是否有效的密钥对(判断是否能否互相加密解密)
  44. * @param $private_key string
  45. * @param $public_key string
  46. * @return bool
  47. */
  48. public function isValidEncryptionKey($private_key, $public_key){
  49. if(!$this->_isValidPublickey($public_key)) {
  50. return false;
  51. }
  52. if(!$this->_isValidPrivatekey($private_key)) {
  53. return false;
  54. }
  55. $data = NOW_TIMESTAMP;
  56. // 使用公钥加密
  57. openssl_public_encrypt($data, $encrypted, openssl_pkey_get_public($public_key));
  58. $encrypted = base64_encode($encrypted);
  59. // 使用秘钥看是否能够解密,能的话,说明就是配对的
  60. openssl_private_decrypt(base64_decode($encrypted), $decrypted, openssl_pkey_get_private($private_key));
  61. if(NOW_TIMESTAMP != $decrypted) {
  62. return false;
  63. }
  64. return true;
  65. }
  66. }