securerandom.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 OC\Security;
  23. use RandomLib;
  24. use Sabre\DAV\Exception;
  25. use OCP\Security\ISecureRandom;
  26. /**
  27. * Class SecureRandom provides a layer around RandomLib to generate
  28. * secure random strings.
  29. *
  30. * Usage:
  31. * \OC::$server->getSecureRandom()->getMediumStrengthGenerator()->generate(10);
  32. *
  33. * @package OC\Security
  34. */
  35. class SecureRandom implements ISecureRandom {
  36. /** @var \RandomLib\Factory */
  37. var $factory;
  38. /** @var \RandomLib\Generator */
  39. var $generator;
  40. function __construct() {
  41. $this->factory = new RandomLib\Factory;
  42. }
  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. */
  52. public function getLowStrengthGenerator() {
  53. $this->generator = $this->factory->getLowStrengthGenerator();
  54. return $this;
  55. }
  56. /**
  57. * Convenience method to get a medium strength random number generator.
  58. *
  59. * Medium Strength should be used for most needs of a cryptographic nature.
  60. * They are strong enough to be used as keys and salts. However, they do
  61. * take some time and resources to generate, so they should not be over-used
  62. *
  63. * @return $this
  64. */
  65. public function getMediumStrengthGenerator() {
  66. $this->generator = $this->factory->getMediumStrengthGenerator();
  67. return $this;
  68. }
  69. /**
  70. * Generate a random string of specified length.
  71. * @param int $length The length of the generated string
  72. * @param string $characters An optional list of characters to use if no characterlist is
  73. * specified all valid base64 characters are used.
  74. * @return string
  75. * @throws \Exception If the generator is not initialized.
  76. */
  77. public function generate($length, $characters = '') {
  78. if(is_null($this->generator)) {
  79. throw new \Exception('Generator is not initialized.');
  80. }
  81. return $this->generator->generateString($length, $characters);
  82. }
  83. }