ISecureRandom.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Fabrizio Steiner <fabrizio.steiner@gmail.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCP\Security;
  28. /**
  29. * Class SecureRandom provides a wrapper around the random_int function to generate
  30. * secure random strings. For PHP 7 the native CSPRNG is used, older versions do
  31. * use a fallback.
  32. *
  33. * Usage:
  34. * \OC::$server->getSecureRandom()->generate(10);
  35. *
  36. * @since 8.0.0
  37. */
  38. interface ISecureRandom {
  39. /**
  40. * Flags for characters that can be used for <code>generate($length, $characters)</code>
  41. * @since 8.0.0
  42. */
  43. public const CHAR_UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  44. /**
  45. * @since 8.0.0
  46. */
  47. public const CHAR_LOWER = 'abcdefghijklmnopqrstuvwxyz';
  48. /**
  49. * @since 8.0.0
  50. */
  51. public const CHAR_DIGITS = '0123456789';
  52. /**
  53. * @since 8.0.0
  54. */
  55. public const CHAR_SYMBOLS = '!\"#$%&\\\'()*+,-./:;<=>?@[\]^_`{|}~';
  56. /**
  57. * @since 12.0.0
  58. */
  59. public const CHAR_ALPHANUMERIC = self::CHAR_UPPER . self::CHAR_LOWER . self::CHAR_DIGITS;
  60. /**
  61. * Characters that can be used for <code>generate($length, $characters)</code>, to
  62. * generate human-readable random strings. Lower- and upper-case characters and digits
  63. * are included. Characters which are ambiguous are excluded, such as I, l, and 1 and so on.
  64. *
  65. * @since 23.0.0
  66. */
  67. public const CHAR_HUMAN_READABLE = 'abcdefgijkmnopqrstwxyzABCDEFGHJKLMNPQRSTWXYZ23456789';
  68. /**
  69. * Generate a random string of specified length.
  70. * @param int $length The length of the generated string
  71. * @param string $characters An optional list of characters to use if no character list is
  72. * specified all valid base64 characters are used.
  73. * @return string
  74. * @since 8.0.0
  75. */
  76. public function generate(int $length,
  77. string $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'): string;
  78. }