ISecureRandom.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. public const CHAR_ALPHANUMERIC = self::CHAR_UPPER . self::CHAR_LOWER . self::CHAR_DIGITS;
  47. /**
  48. * Characters that can be used for <code>generate($length, $characters)</code>, to
  49. * generate human readable random strings. Lower- and upper-case characters and digits
  50. * are included. Characters which are ambiguous are excluded, such as I, l, and 1 and so on.
  51. */
  52. public const CHAR_HUMAN_READABLE = 'abcdefgijkmnopqrstwxyzABCDEFGHJKLMNPQRSTWXYZ23456789';
  53. /**
  54. * Generate a random string of specified length.
  55. * @param int $length The length of the generated string
  56. * @param string $characters An optional list of characters to use if no character list is
  57. * specified all valid base64 characters are used.
  58. * @return string
  59. * @since 8.0.0
  60. */
  61. public function generate(int $length,
  62. string $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'): string;
  63. }