SecurityProvider.php 980 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. /* ===========================================================================
  3. * Copyright (c) 2018-2021 Zindex Software
  4. *
  5. * Licensed under the MIT License
  6. * =========================================================================== */
  7. namespace Opis\Closure;
  8. class SecurityProvider implements ISecurityProvider
  9. {
  10. /** @var string */
  11. protected $secret;
  12. /**
  13. * SecurityProvider constructor.
  14. * @param string $secret
  15. */
  16. public function __construct($secret)
  17. {
  18. $this->secret = $secret;
  19. }
  20. /**
  21. * @inheritdoc
  22. */
  23. public function sign($closure)
  24. {
  25. return array(
  26. 'closure' => $closure,
  27. 'hash' => base64_encode(hash_hmac('sha256', $closure, $this->secret, true)),
  28. );
  29. }
  30. /**
  31. * @inheritdoc
  32. */
  33. public function verify(array $data)
  34. {
  35. return base64_encode(hash_hmac('sha256', $data['closure'], $this->secret, true)) === $data['hash'];
  36. }
  37. }