Signature.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace components\components\messageQueue;
  3. use yii\base\Component;
  4. use yii\base\InvalidParamException;
  5. /**
  6. * Signature
  7. * 签名
  8. * ---------
  9. * @author Verdient。
  10. * @version 1.0.0
  11. */
  12. class Signature extends Component {
  13. /**
  14. * @var public String $key
  15. * 签名密钥
  16. * -----------------------
  17. * @author Verdient。
  18. */
  19. public $key;
  20. /**
  21. * @var public String $verb
  22. * 访问方式
  23. * ------------------------
  24. * @author Verdient。
  25. */
  26. public $verb;
  27. /**
  28. * @var public String $contentType
  29. * 消息体类型
  30. * -------------------------------
  31. * @author Verdient。
  32. */
  33. public $contentType = 'application/json';
  34. /**
  35. * @var public String $signatureMethod
  36. * 签名方法
  37. * -----------------------------------
  38. * @author Verdient。
  39. */
  40. public $signatureMethod = 'MD5';
  41. /**
  42. * @var public String $signatureVersion
  43. * 签名版本
  44. * ------------------------------------
  45. * @author Verdient。
  46. */
  47. public $signatureVersion = '1.0';
  48. /**
  49. * @var public String $_content
  50. * 消息体
  51. * ----------------------------
  52. * @author Verdient。
  53. */
  54. public $_content = [];
  55. /**
  56. * @var protected String $_contentMd5
  57. * 消息体MD5值
  58. * ----------------------------------
  59. * @author Verdient。
  60. */
  61. protected $_contentMd5 = null;
  62. /**
  63. * @var protected String $_date
  64. * 日期
  65. * ----------------------------
  66. * @author Verdient。
  67. */
  68. protected $_date;
  69. /**
  70. * @var protected String $_signature
  71. * 签名
  72. * ---------------------------------
  73. * @author Verdient。
  74. */
  75. protected $_signature;
  76. /**
  77. * setContent(Array $content)
  78. * 设置消息体
  79. * --------------------------
  80. * @param Array $content 消息体
  81. * ----------------------------
  82. * @author Verdient。
  83. */
  84. public function setContent(Array $content){
  85. static::_prepareContent($content);
  86. $this->_content = $content;
  87. }
  88. /**
  89. * getContent()
  90. * 获取消息体
  91. * ------------
  92. * @return Array
  93. * @author Verdient。
  94. */
  95. public function getContent(){
  96. return $this->_content;
  97. }
  98. /**
  99. * setDate(Mixed $date)
  100. * 设置日期
  101. * --------------------
  102. * @param Mixed $date 日期
  103. * -----------------------
  104. * @author Verdient。
  105. */
  106. public function setDate($date){
  107. if(is_numeric($date) && ctype_digit($str) && $str <= 2147483647){
  108. $this->_date = gmdate('D, d M Y H:i:s \G\M\T', $date);
  109. }else if(is_string($date)){
  110. $this->_date = gmdate('D, d M Y H:i:s \G\M\T', strtotime($date));
  111. }else{
  112. throw new InvalidParamException('Data must be a timestamp or date string');
  113. }
  114. }
  115. /**
  116. * getDate()
  117. * 获取日期
  118. * ---------
  119. * @return String
  120. * @author Verdient。
  121. */
  122. public function getDate(){
  123. if(!$this->_date){
  124. $this->_date = gmdate('D, d M Y H:i:s \G\M\T');
  125. }
  126. return $this->_date;
  127. }
  128. /**
  129. * getContentMd5()
  130. * 获取消息体MD5值
  131. * ---------------
  132. * @return String
  133. * @author Verdient。
  134. */
  135. public function getContentMd5(){
  136. if(!$this->_contentMd5 !== null){
  137. if(!is_array($this->content)){
  138. throw new InvalidParamException('Content must be an Array');
  139. }
  140. $this->_contentMd5 = empty($this->content) ? '' : md5(json_encode($this->content));
  141. }
  142. return $this->_contentMd5;
  143. }
  144. /**
  145. * getSignature()
  146. * 获取签名
  147. * --------------
  148. * @return String
  149. * @author Verdient。
  150. */
  151. public function getSignature(){
  152. if(!$this->_signature){
  153. if(!$this->key){
  154. throw new InvalidParamException('Signature key must be set');
  155. }
  156. $this->_signature = md5(base64_encode(hash_hmac("sha1", $this->_buildSignatureString(), $this->key . '&', true)));
  157. }
  158. return $this->_signature;
  159. }
  160. /**
  161. * _buildSignatureString()
  162. * 构建待签名的字符串
  163. * -----------------------
  164. * @return String
  165. * @author Verdient。
  166. */
  167. protected function _buildSignatureString(){
  168. return strtoupper($this->verb) .
  169. "\n\n" .
  170. $this->contentMd5 .
  171. "\n" .
  172. $this->contentType .
  173. "\n" .
  174. $this->date.
  175. "\n" .
  176. $this->signatureMethod .
  177. "\n" .
  178. $this->signatureVersion .
  179. "\n\n" .
  180. $this->key .
  181. "\n";
  182. }
  183. /**
  184. * _prepareContent(Array &$content)
  185. * 准备消息体
  186. * --------------------------------
  187. * @param Array &$content 消息体
  188. * ----------------------------
  189. * @author Verdient。
  190. */
  191. protected static function _prepareContent(&$content){
  192. ksort($content);
  193. foreach($content as $key => $value){
  194. if(empty($value)){
  195. unset($content[$key]);
  196. }else if(is_array($value)){
  197. static::_prepareContent($content[$key]);
  198. }
  199. }
  200. }
  201. }