ShaHmac256WithRsaSignature.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace AlibabaCloud\Client\Signature;
  3. use Exception;
  4. use AlibabaCloud\Client\SDK;
  5. use AlibabaCloud\Client\Exception\ClientException;
  6. /**
  7. * Class ShaHmac256WithRsaSignature
  8. *
  9. * @package AlibabaCloud\Signature
  10. */
  11. class ShaHmac256WithRsaSignature extends Signature implements SignatureInterface
  12. {
  13. /**
  14. * @return string
  15. */
  16. public function getMethod()
  17. {
  18. return 'SHA256withRSA';
  19. }
  20. /**
  21. * @return string
  22. */
  23. public function getType()
  24. {
  25. return 'PRIVATEKEY';
  26. }
  27. /**
  28. * @return string
  29. */
  30. public function getVersion()
  31. {
  32. return '1.0';
  33. }
  34. /**
  35. * @param string $string
  36. * @param string $privateKey
  37. *
  38. * @return string
  39. * @throws ClientException
  40. */
  41. public function sign($string, $privateKey)
  42. {
  43. $binarySignature = '';
  44. try {
  45. openssl_sign(
  46. $string,
  47. $binarySignature,
  48. $privateKey,
  49. \OPENSSL_ALGO_SHA256
  50. );
  51. } catch (Exception $exception) {
  52. throw new ClientException(
  53. $exception->getMessage(),
  54. SDK::INVALID_CREDENTIAL
  55. );
  56. }
  57. return base64_encode($binarySignature);
  58. }
  59. }