SignUtil.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\utils\Ocr\Util;
  8. /*
  9. * Licensed to the Apache Software Foundation (ASF) under one
  10. * or more contributor license agreements. See the NOTICE file
  11. * distributed with this work for additional information
  12. * regarding copyright ownership. The ASF licenses this file
  13. * to you under the Apache License, Version 2.0 (the
  14. * "License"); you may not use this file except in compliance
  15. * with the License. You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing,
  20. * software distributed under the License is distributed on an
  21. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  22. * KIND, either express or implied. See the License for the
  23. * specific language governing permissions and limitations
  24. * under the License.
  25. */
  26. use app\utils\Ocr\Constant\Constants;
  27. use app\utils\Ocr\Constant\HttpHeader;
  28. use app\utils\Ocr\Constant\SystemHeader;
  29. /**
  30. *签名处理
  31. */
  32. class SignUtil
  33. {
  34. /**
  35. * 构建待签名
  36. */
  37. public static function Sign($path, $method, $secret, &$headers, $querys, $bodys, $signHeaderPrefixList)
  38. {
  39. $signStr = self::BuildStringToSign($path, $method, $headers, $querys, $bodys, $signHeaderPrefixList);
  40. return base64_encode(hash_hmac('sha256', $signStr, $secret, true));
  41. }
  42. /**
  43. * 构建待签名path+(header+query+body)
  44. */
  45. private static function BuildStringToSign($path, $method, &$headers, $querys, $bodys, $signHeaderPrefixList)
  46. {
  47. $sb = "";
  48. $sb.= strtoupper($method);
  49. $sb.= Constants::LF;
  50. if (array_key_exists(HttpHeader::HTTP_HEADER_ACCEPT, $headers) && null != $headers[HttpHeader::HTTP_HEADER_ACCEPT]) {
  51. $sb.= $headers[HttpHeader::HTTP_HEADER_ACCEPT];
  52. }
  53. $sb.= Constants::LF;
  54. if (array_key_exists(HttpHeader::HTTP_HEADER_CONTENT_MD5, $headers) && null != $headers[HttpHeader::HTTP_HEADER_ACCEPT]) {
  55. $sb.= $headers[HttpHeader::HTTP_HEADER_CONTENT_MD5];
  56. }
  57. $sb.= Constants::LF;
  58. if (array_key_exists(HttpHeader::HTTP_HEADER_CONTENT_TYPE, $headers) && null != $headers[HttpHeader::HTTP_HEADER_ACCEPT]) {
  59. $sb.= $headers[HttpHeader::HTTP_HEADER_CONTENT_TYPE];
  60. }
  61. $sb.= Constants::LF;
  62. if (array_key_exists(HttpHeader::HTTP_HEADER_DATE, $headers) && null != $headers[HttpHeader::HTTP_HEADER_ACCEPT]) {
  63. $sb.= $headers[HttpHeader::HTTP_HEADER_DATE];
  64. }
  65. $sb.= Constants::LF;
  66. $sb.= self::BuildHeaders($headers, $signHeaderPrefixList);
  67. $sb.= self::BuildResource($path, $querys, $bodys);
  68. return $sb;
  69. }
  70. /**
  71. * 构建待签名Path+Query+FormParams
  72. */
  73. private static function BuildResource($path, $querys, $bodys)
  74. {
  75. $sb = "";
  76. if (0 < strlen($path))
  77. {
  78. $sb.=$path;
  79. }
  80. $sbParam = "";
  81. $sortParams = array();
  82. //query参与签名
  83. if (is_array($querys)) {
  84. foreach ($querys as $itemKey => $itemValue) {
  85. if (0 < strlen($itemKey)) {
  86. $sortParams[$itemKey] = $itemValue;
  87. }
  88. }
  89. }
  90. //body参与签名
  91. if (is_array($bodys)) {
  92. foreach ($bodys as $itemKey => $itemValue) {
  93. if (0 < strlen($itemKey)) {
  94. $sortParams[$itemKey] = $itemValue;
  95. }
  96. }
  97. }
  98. //排序
  99. ksort($sortParams);
  100. //参数Key
  101. foreach ($sortParams as $itemKey => $itemValue) {
  102. if (0 < strlen($itemKey)) {
  103. if (0 < strlen($sbParam)) {
  104. $sbParam.="&";
  105. }
  106. $sbParam.=$itemKey;
  107. if (null != $itemValue)
  108. {
  109. if (0 < strlen($itemValue)) {
  110. $sbParam.="=";
  111. $sbParam.=$itemValue;
  112. }
  113. }
  114. }
  115. }
  116. if (0 < strlen($sbParam)) {
  117. $sb.="?";
  118. $sb.=$sbParam;
  119. }
  120. return $sb;
  121. }
  122. /**
  123. * 构建待签名Http头
  124. *
  125. * @param headers 请求中所有的Http头
  126. * @param signHeaderPrefixList 自定义参与签名Header前缀
  127. * @return 待签名Http头
  128. */
  129. private static function BuildHeaders(&$headers, $signHeaderPrefixList)
  130. {
  131. $sb = "";
  132. if (null != $signHeaderPrefixList)
  133. {
  134. //剔除X-Ca-Signature/X-Ca-Signature-Headers/Accept/Content-MD5/Content-Type/Date
  135. unset($signHeaderPrefixList[SystemHeader::X_CA_SIGNATURE]);
  136. unset($signHeaderPrefixList[HttpHeader::HTTP_HEADER_ACCEPT]);
  137. unset($signHeaderPrefixList[HttpHeader::HTTP_HEADER_CONTENT_MD5]);
  138. unset($signHeaderPrefixList[HttpHeader::HTTP_HEADER_CONTENT_TYPE]);
  139. unset($signHeaderPrefixList[HttpHeader::HTTP_HEADER_DATE]);
  140. ksort($signHeaderPrefixList);
  141. if (is_array($headers)) {
  142. ksort($headers);
  143. $signHeadersStringBuilder = "";
  144. foreach ($headers as $itemKey => $itemValue)
  145. {
  146. if (self::IsHeaderToSign($itemKey, $signHeaderPrefixList))
  147. {
  148. $sb.=$itemKey;
  149. $sb.=Constants::SPE2;
  150. if (0 < strlen($itemValue)) {
  151. $sb.=$itemValue;
  152. }
  153. $sb.=Constants::LF;
  154. if (0 < strlen($signHeadersStringBuilder))
  155. {
  156. $signHeadersStringBuilder.= Constants::SPE1;
  157. }
  158. $signHeadersStringBuilder.= $itemKey;
  159. }
  160. }
  161. $headers[SystemHeader::X_CA_SIGNATURE_HEADERS] = $signHeadersStringBuilder;
  162. }
  163. }
  164. return $sb;
  165. }
  166. /**
  167. * Http头是否参与签名
  168. * return
  169. */
  170. private static function IsHeaderToSign($headerName, $signHeaderPrefixList)
  171. {
  172. if (NULL == $headerName) {
  173. return false;
  174. }
  175. if (0 == strlen($headerName)) {
  176. return false;
  177. }
  178. if (1 == strpos("$".$headerName, Constants::CA_HEADER_TO_SIGN_PREFIX_SYSTEM)) {
  179. return true;
  180. }
  181. if (!is_array($signHeaderPrefixList) || empty($signHeaderPrefixList) ) {
  182. return false;
  183. }
  184. if (array_key_exists($headerName, $signHeaderPrefixList)) {
  185. return true;
  186. }
  187. return false;
  188. }
  189. }