| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\utils\Ocr\Util;
- /*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
- use app\utils\Ocr\Constant\Constants;
- use app\utils\Ocr\Constant\HttpHeader;
- use app\utils\Ocr\Constant\SystemHeader;
- /**
- *签名处理
- */
- class SignUtil
- {
- /**
- * 构建待签名
- */
- public static function Sign($path, $method, $secret, &$headers, $querys, $bodys, $signHeaderPrefixList)
- {
- $signStr = self::BuildStringToSign($path, $method, $headers, $querys, $bodys, $signHeaderPrefixList);
- return base64_encode(hash_hmac('sha256', $signStr, $secret, true));
- }
- /**
- * 构建待签名path+(header+query+body)
- */
- private static function BuildStringToSign($path, $method, &$headers, $querys, $bodys, $signHeaderPrefixList)
- {
- $sb = "";
- $sb.= strtoupper($method);
- $sb.= Constants::LF;
- if (array_key_exists(HttpHeader::HTTP_HEADER_ACCEPT, $headers) && null != $headers[HttpHeader::HTTP_HEADER_ACCEPT]) {
- $sb.= $headers[HttpHeader::HTTP_HEADER_ACCEPT];
- }
- $sb.= Constants::LF;
- if (array_key_exists(HttpHeader::HTTP_HEADER_CONTENT_MD5, $headers) && null != $headers[HttpHeader::HTTP_HEADER_ACCEPT]) {
- $sb.= $headers[HttpHeader::HTTP_HEADER_CONTENT_MD5];
- }
- $sb.= Constants::LF;
- if (array_key_exists(HttpHeader::HTTP_HEADER_CONTENT_TYPE, $headers) && null != $headers[HttpHeader::HTTP_HEADER_ACCEPT]) {
- $sb.= $headers[HttpHeader::HTTP_HEADER_CONTENT_TYPE];
- }
- $sb.= Constants::LF;
- if (array_key_exists(HttpHeader::HTTP_HEADER_DATE, $headers) && null != $headers[HttpHeader::HTTP_HEADER_ACCEPT]) {
- $sb.= $headers[HttpHeader::HTTP_HEADER_DATE];
- }
- $sb.= Constants::LF;
- $sb.= self::BuildHeaders($headers, $signHeaderPrefixList);
- $sb.= self::BuildResource($path, $querys, $bodys);
- return $sb;
- }
- /**
- * 构建待签名Path+Query+FormParams
- */
- private static function BuildResource($path, $querys, $bodys)
- {
- $sb = "";
- if (0 < strlen($path))
- {
- $sb.=$path;
- }
- $sbParam = "";
- $sortParams = array();
- //query参与签名
- if (is_array($querys)) {
- foreach ($querys as $itemKey => $itemValue) {
- if (0 < strlen($itemKey)) {
- $sortParams[$itemKey] = $itemValue;
- }
- }
- }
- //body参与签名
- if (is_array($bodys)) {
- foreach ($bodys as $itemKey => $itemValue) {
- if (0 < strlen($itemKey)) {
- $sortParams[$itemKey] = $itemValue;
- }
- }
- }
- //排序
- ksort($sortParams);
- //参数Key
- foreach ($sortParams as $itemKey => $itemValue) {
- if (0 < strlen($itemKey)) {
- if (0 < strlen($sbParam)) {
- $sbParam.="&";
- }
- $sbParam.=$itemKey;
- if (null != $itemValue)
- {
- if (0 < strlen($itemValue)) {
- $sbParam.="=";
- $sbParam.=$itemValue;
- }
- }
- }
- }
- if (0 < strlen($sbParam)) {
- $sb.="?";
- $sb.=$sbParam;
- }
- return $sb;
- }
-
- /**
- * 构建待签名Http头
- *
- * @param headers 请求中所有的Http头
- * @param signHeaderPrefixList 自定义参与签名Header前缀
- * @return 待签名Http头
- */
- private static function BuildHeaders(&$headers, $signHeaderPrefixList)
- {
- $sb = "";
- if (null != $signHeaderPrefixList)
- {
- //剔除X-Ca-Signature/X-Ca-Signature-Headers/Accept/Content-MD5/Content-Type/Date
- unset($signHeaderPrefixList[SystemHeader::X_CA_SIGNATURE]);
- unset($signHeaderPrefixList[HttpHeader::HTTP_HEADER_ACCEPT]);
- unset($signHeaderPrefixList[HttpHeader::HTTP_HEADER_CONTENT_MD5]);
- unset($signHeaderPrefixList[HttpHeader::HTTP_HEADER_CONTENT_TYPE]);
- unset($signHeaderPrefixList[HttpHeader::HTTP_HEADER_DATE]);
- ksort($signHeaderPrefixList);
-
- if (is_array($headers)) {
- ksort($headers);
- $signHeadersStringBuilder = "";
- foreach ($headers as $itemKey => $itemValue)
- {
- if (self::IsHeaderToSign($itemKey, $signHeaderPrefixList))
- {
- $sb.=$itemKey;
- $sb.=Constants::SPE2;
- if (0 < strlen($itemValue)) {
- $sb.=$itemValue;
- }
- $sb.=Constants::LF;
- if (0 < strlen($signHeadersStringBuilder))
- {
- $signHeadersStringBuilder.= Constants::SPE1;
- }
- $signHeadersStringBuilder.= $itemKey;
- }
- }
- $headers[SystemHeader::X_CA_SIGNATURE_HEADERS] = $signHeadersStringBuilder;
- }
- }
- return $sb;
- }
- /**
- * Http头是否参与签名
- * return
- */
- private static function IsHeaderToSign($headerName, $signHeaderPrefixList)
- {
- if (NULL == $headerName) {
- return false;
- }
- if (0 == strlen($headerName)) {
- return false;
- }
- if (1 == strpos("$".$headerName, Constants::CA_HEADER_TO_SIGN_PREFIX_SYSTEM)) {
- return true;
- }
- if (!is_array($signHeaderPrefixList) || empty($signHeaderPrefixList) ) {
- return false;
- }
- if (array_key_exists($headerName, $signHeaderPrefixList)) {
- return true;
- }
- return false;
- }
- }
|