ISecureRandom.php 3.2 KB

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