1
0

ISecureRandom.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OCP\Security;
  9. /**
  10. * Class SecureRandom provides a wrapper around the random_int function to generate
  11. * secure random strings. For PHP 7 the native CSPRNG is used, older versions do
  12. * use a fallback.
  13. *
  14. * Usage:
  15. * \OC::$server->get(ISecureRandom::class)->generate(10);
  16. *
  17. * @since 8.0.0
  18. */
  19. interface ISecureRandom {
  20. /**
  21. * Flags for characters that can be used for <code>generate($length, $characters)</code>
  22. * @since 8.0.0
  23. */
  24. public const CHAR_UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  25. /**
  26. * @since 8.0.0
  27. */
  28. public const CHAR_LOWER = 'abcdefghijklmnopqrstuvwxyz';
  29. /**
  30. * @since 8.0.0
  31. */
  32. public const CHAR_DIGITS = '0123456789';
  33. /**
  34. * @since 8.0.0
  35. */
  36. public const CHAR_SYMBOLS = '!\"#$%&\\\'()*+,-./:;<=>?@[\]^_`{|}~';
  37. /**
  38. * @since 12.0.0
  39. */
  40. public const CHAR_ALPHANUMERIC = self::CHAR_UPPER . self::CHAR_LOWER . self::CHAR_DIGITS;
  41. /**
  42. * Characters that can be used for <code>generate($length, $characters)</code>, to
  43. * generate human-readable random strings. Lower- and upper-case characters and digits
  44. * are included. Characters which are ambiguous are excluded, such as I, l, and 1 and so on.
  45. *
  46. * @since 23.0.0
  47. */
  48. public const CHAR_HUMAN_READABLE = 'abcdefgijkmnopqrstwxyzABCDEFGHJKLMNPQRSTWXYZ23456789';
  49. /**
  50. * Generate a random string of specified length.
  51. * @param int $length The length of the generated string
  52. * @param string $characters An optional list of characters to use if no character list is
  53. * specified all valid base64 characters are used.
  54. * @return string
  55. * @since 8.0.0
  56. */
  57. public function generate(int $length,
  58. string $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'): string;
  59. }