ISecureRandom.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. * Convenience method to get a low strength random number generator.
  54. *
  55. * Low Strength should be used anywhere that random strings are needed
  56. * in a non-cryptographical setting. They are not strong enough to be
  57. * used as keys or salts. They are however useful for one-time use tokens.
  58. *
  59. * @return $this
  60. * @since 8.0.0
  61. * @deprecated 9.0.0 Use \OC\Security\SecureRandom::generate directly or random_bytes() / random_int()
  62. */
  63. public function getLowStrengthGenerator();
  64. /**
  65. * Convenience method to get a medium strength random number generator.
  66. *
  67. * Medium Strength should be used for most needs of a cryptographic nature.
  68. * They are strong enough to be used as keys and salts. However, they do
  69. * take some time and resources to generate, so they should not be over-used
  70. *
  71. * @return $this
  72. * @since 8.0.0
  73. * @deprecated 9.0.0 Use \OC\Security\SecureRandom::generate directly or random_bytes() / random_int()
  74. */
  75. public function getMediumStrengthGenerator();
  76. /**
  77. * Generate a random string of specified length.
  78. * @param int $length The length of the generated string
  79. * @param string $characters An optional list of characters to use if no character list is
  80. * specified all valid base64 characters are used.
  81. * @return string
  82. * @since 8.0.0
  83. */
  84. public function generate(int $length,
  85. string $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'): string;
  86. }