Sign.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /*
  3. * Copyright (c) 2017-2018 THL A29 Limited, a Tencent company. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing,
  12. * software distributed under the License is distributed on an
  13. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. * KIND, either express or implied. See the License for the
  15. * specific language governing permissions and limitations
  16. * under the License.
  17. */
  18. namespace TencentCloud\Common;
  19. use TencentCloud\Common\Exception\TencentCloudSDKException;
  20. /**
  21. * 签名类,禁止client引用
  22. * @package TencentCloud\Common
  23. * @throws TencentCloudSDKException
  24. */
  25. class Sign
  26. {
  27. /**
  28. * @throws TencentCloudSDKException
  29. */
  30. public static function sign($secretKey, $signStr, $signMethod)
  31. {
  32. $signMethodMap = ["HmacSHA1" => "SHA1", "HmacSHA256" => "SHA256"];
  33. if (!array_key_exists($signMethod, $signMethodMap)) {
  34. throw new TencentCloudSDKException("signMethod invalid", "signMethod only support (HmacSHA1, HmacSHA256)");
  35. }
  36. $signature = base64_encode(hash_hmac($signMethodMap[$signMethod], $signStr, $secretKey, true));
  37. return $signature;
  38. }
  39. public static function signTC3($skey, $date, $service, $str2sign)
  40. {
  41. $dateKey = hash_hmac("SHA256", $date, "TC3".$skey, true);
  42. $serviceKey = hash_hmac("SHA256", $service, $dateKey, true);
  43. $reqKey = hash_hmac("SHA256", "tc3_request", $serviceKey, true);
  44. return hash_hmac("SHA256", $str2sign, $reqKey);
  45. }
  46. }