ISecureRandom.php 3.3 KB

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