ISecureRandom.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Fabrizio Steiner <fabrizio.steiner@gmail.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCP\Security;
  27. /**
  28. * Class SecureRandom provides a wrapper around the random_int function to generate
  29. * secure random strings. For PHP 7 the native CSPRNG is used, older versions do
  30. * use a fallback.
  31. *
  32. * Usage:
  33. * \OC::$server->getSecureRandom()->generate(10);
  34. *
  35. * @package OCP\Security
  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. const CHAR_UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  43. const CHAR_LOWER = 'abcdefghijklmnopqrstuvwxyz';
  44. const CHAR_DIGITS = '0123456789';
  45. 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. 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. }