isecurerandom.php 2.5 KB

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