DingController.Class.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace JinDouYun\Controller;
  3. /**
  4. * PHP7.1及其之上版本的回调加解密类库
  5. * 该版本依赖openssl_encrypt方法加解密,注意版本依赖 (PHP 5 >= 5.3.0, PHP 7)
  6. */
  7. class DingController
  8. {
  9. /**
  10. * @param token 钉钉开放平台上,开发者设置的token
  11. * @param encodingAesKey 钉钉开放台上,开发者设置的EncodingAESKey
  12. * @param corpId 企业自建应用-事件订阅, 使用appKey
  13. * 企业自建应用-注册回调地址, 使用corpId
  14. * 第三方企业应用, 使用suiteKey
  15. */
  16. private $m_token;
  17. private $m_encodingAesKey;
  18. private $m_corpId;
  19. //注意这里修改为构造函数
  20. function __construct($token, $encodingAesKey, $ownerKey)
  21. {
  22. $this->m_token = $token;
  23. $this->m_encodingAesKey = $encodingAesKey;
  24. $this->m_corpId = $ownerKey;
  25. }
  26. public function getEncryptedMap($plain){
  27. $timeStamp = time();
  28. $pc = new Prpcrypt($this->m_encodingAesKey);
  29. $nonce= $pc->getRandomStr();
  30. return $this->getEncryptedMapDetail($plain, $timeStamp, $nonce);
  31. }
  32. /**
  33. * 加密回调信息
  34. */
  35. public function getEncryptedMapDetail($plain, $timeStamp, $nonce)
  36. {
  37. $pc = new Prpcrypt($this->m_encodingAesKey);
  38. $array = $pc->encrypt($plain, $this->m_corpId);
  39. $ret = $array[0];
  40. if ($ret != 0) {
  41. //return $ret;
  42. // return ['ErrorCode'=>$ret, 'data' => ''];
  43. throw new \Exception('AES加密错误',ErrorCode::$EncryptAESError);
  44. }
  45. if ($timeStamp == null) {
  46. $timeStamp = time();
  47. }
  48. $encrypt = $array[1];
  49. $sha1 = new SHA1;
  50. $array = $sha1->getSHA1($this->m_token, $timeStamp, $nonce, $encrypt);
  51. $ret = $array[0];
  52. if ($ret != 0) {
  53. //return $ret;
  54. throw new \Exception('ComputeSignatureError',ErrorCode::$ComputeSignatureError);
  55. }
  56. $signature = $array[1];
  57. $encryptMsg = json_encode(array(
  58. "msg_signature" => $signature,
  59. "encrypt" => $encrypt,
  60. "timeStamp" => $timeStamp,
  61. "nonce" => $nonce
  62. ));
  63. return $encryptMsg;
  64. }
  65. /**
  66. * 解密回调信息
  67. */
  68. public function getDecryptMsg($signature, $timeStamp = null, $nonce, $encrypt)
  69. {
  70. if (strlen($this->m_encodingAesKey) != 43) {
  71. //return ErrorCode::$IllegalAesKey;
  72. return ['ErrorCode'=>ErrorCode::$IllegalAesKey, 'data' => ''];
  73. // throw new \Exception('IllegalAesKey',ErrorCode::$IllegalAesKey);
  74. }
  75. $pc = new Prpcrypt($this->m_encodingAesKey);
  76. if ($timeStamp == null) {
  77. $timeStamp = time();
  78. }
  79. $sha1 = new SHA1;
  80. $array = $sha1->getSHA1($this->m_token, $timeStamp, $nonce, $encrypt);
  81. $ret = $array[0];
  82. if ($ret != 0) {
  83. //return $ret;
  84. return ['ErrorCode'=>$ret, 'data' => ''];
  85. // throw new \Exception('ComputeSignatureError',ErrorCode::$ComputeSignatureError);
  86. }
  87. $verifySignature = $array[1];
  88. if ($verifySignature != $signature) {
  89. // return ErrorCode::$ValidateSignatureError;
  90. return ['ErrorCode'=>ErrorCode::$ValidateSignatureError, 'data' => ''];
  91. // throw new \Exception('ValidateSignatureError',ErrorCode::$ValidateSignatureError);
  92. }
  93. $result = $pc->decrypt($encrypt, $this->m_corpId);
  94. if ($result[0] != 0) {
  95. //return $result[0];
  96. return ['ErrorCode'=>$result[0], 'data' => ''];
  97. // throw new \Exception('DecryptAESError',ErrorCode::$DecryptAESError);
  98. }
  99. $decryptMsg = $result[1];
  100. //return ErrorCode::$OK;
  101. return $decryptMsg;
  102. }
  103. }
  104. ?>