You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.4 KiB

3 years ago
  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. */
  42. public const CHAR_UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  43. public const CHAR_LOWER = 'abcdefghijklmnopqrstuvwxyz';
  44. public const CHAR_DIGITS = '0123456789';
  45. public const CHAR_SYMBOLS = '!\"#$%&\\\'()*+,-./:;<=>?@[\]^_`{|}~';
  46. /**
  47. * Characters that can be used for <code>generate($length, $characters)</code>, to
  48. * generate human readable random strings. Lower- and upper-case characters and digits
  49. * are included. Characters which are ambiguous are excluded, such as I, l, and 1 and so on.
  50. */
  51. public const CHAR_HUMAN_READABLE = 'abcdefgijkmnopqrstwxyzABCDEFGHJKLMNPQRSTWXYZ23456789';
  52. /**
  53. * Generate a random string of specified length.
  54. * @param int $length The length of the generated string
  55. * @param string $characters An optional list of characters to use if no character list is
  56. * specified all valid base64 characters are used.
  57. * @return string
  58. * @since 8.0.0
  59. */
  60. public function generate(int $length,
  61. string $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'): string;
  62. }