ShaHmac256WithRsaSignature.php 1.1 KB

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