SecureRandom.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 OC\Security;
  24. use RandomLib;
  25. use Sabre\DAV\Exception;
  26. use OCP\Security\ISecureRandom;
  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. * @package OC\Security
  35. */
  36. class SecureRandom implements ISecureRandom {
  37. /**
  38. * Convenience method to get a low strength random number generator.
  39. *
  40. * Low Strength should be used anywhere that random strings are needed
  41. * in a non-cryptographical setting. They are not strong enough to be
  42. * used as keys or salts. They are however useful for one-time use tokens.
  43. *
  44. * @deprecated 9.0.0 Use \OC\Security\SecureRandom::generate directly or random_bytes() / random_int()
  45. * @return $this
  46. */
  47. public function getLowStrengthGenerator() {
  48. return $this;
  49. }
  50. /**
  51. * Convenience method to get a medium strength random number generator.
  52. *
  53. * Medium Strength should be used for most needs of a cryptographic nature.
  54. * They are strong enough to be used as keys and salts. However, they do
  55. * take some time and resources to generate, so they should not be over-used
  56. *
  57. * @deprecated 9.0.0 Use \OC\Security\SecureRandom::generate directly or random_bytes() / random_int()
  58. * @return $this
  59. */
  60. public function getMediumStrengthGenerator() {
  61. return $this;
  62. }
  63. /**
  64. * Generate a random string of specified length.
  65. * @param int $length The length of the generated string
  66. * @param string $characters An optional list of characters to use if no character list is
  67. * specified all valid base64 characters are used.
  68. * @return string
  69. */
  70. public function generate($length,
  71. $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/') {
  72. $maxCharIndex = strlen($characters) - 1;
  73. $randomString = '';
  74. while($length > 0) {
  75. $randomNumber = \random_int(0, $maxCharIndex);
  76. $randomString .= $characters[$randomNumber];
  77. $length--;
  78. }
  79. return $randomString;
  80. }
  81. }