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.

ISecureRandom.php 2.8KB

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