CryptoUtil.php 806 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. namespace PhpZip\Util;
  3. use PhpZip\Exception\RuntimeException;
  4. use PhpZip\Exception\ZipException;
  5. /**
  6. * Crypto Utils
  7. */
  8. class CryptoUtil
  9. {
  10. /**
  11. * Returns random bytes.
  12. *
  13. * @param int $length
  14. * @return string
  15. * @throws RuntimeException
  16. */
  17. public static final function randomBytes($length)
  18. {
  19. $length = (int)$length;
  20. if (function_exists('random_bytes')) {
  21. return random_bytes($length);
  22. } elseif (function_exists('openssl_random_pseudo_bytes')) {
  23. return openssl_random_pseudo_bytes($length);
  24. } elseif (function_exists('mcrypt_create_iv')) {
  25. return mcrypt_create_iv($length);
  26. } else {
  27. throw new RuntimeException('Extension openssl or mcrypt not loaded');
  28. }
  29. }
  30. }