Secure.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Polyfill\Php82\Random\Engine;
  11. use Random\RandomException;
  12. use Symfony\Polyfill\Php82\NoDynamicProperties;
  13. /**
  14. * @author Tim Düsterhus <tim@bastelstu.be>
  15. * @author Anton Smirnov <sandfox@sandfox.me>
  16. *
  17. * @internal
  18. */
  19. class Secure
  20. {
  21. use NoDynamicProperties;
  22. public function generate(): string
  23. {
  24. try {
  25. return random_bytes(\PHP_INT_SIZE);
  26. } catch (\Exception $e) {
  27. throw new RandomException($e->getMessage(), $e->getCode(), $e->getPrevious());
  28. }
  29. }
  30. public function __sleep(): array
  31. {
  32. throw new \Exception("Serialization of 'Random\Engine\Secure' is not allowed");
  33. }
  34. public function __wakeup(): void
  35. {
  36. throw new \Exception("Unserialization of 'Random\Engine\Secure' is not allowed");
  37. }
  38. public function __clone()
  39. {
  40. throw new \Error('Trying to clone an uncloneable object of class Random\Engine\Secure');
  41. }
  42. }