SHA1.php 871 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace WeWork;
  3. /**
  4. * SHA1 class
  5. *
  6. * 计算公众平台的消息签名接口.
  7. */
  8. class SHA1
  9. {
  10. /**
  11. * 用SHA1算法生成安全签名
  12. *
  13. * @param string $token 票据
  14. * @param string $timestamp 时间戳
  15. * @param string $nonce 随机字符串
  16. * @param string $encrypt_msg 密文消息
  17. *
  18. * @return array
  19. */
  20. public function getSHA1($token, $timestamp, $nonce, $encrypt_msg)
  21. {
  22. //排序
  23. try {
  24. $array = array($encrypt_msg, $token, $timestamp, $nonce);
  25. sort($array, SORT_STRING);
  26. $str = implode($array);
  27. return array(ErrorCode::$OK, sha1($str));
  28. } catch (\Exception $e) {
  29. print $e . "\n";
  30. return array(ErrorCode::$ComputeSignatureError, null);
  31. }
  32. }
  33. }