CsrfTokenGenerator.php 733 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OC\Security\CSRF;
  9. use OCP\Security\ISecureRandom;
  10. /**
  11. * Class CsrfTokenGenerator is used to generate a cryptographically secure
  12. * pseudo-random number for the token.
  13. *
  14. * @package OC\Security\CSRF
  15. */
  16. class CsrfTokenGenerator {
  17. public function __construct(
  18. private ISecureRandom $random,
  19. ) {
  20. }
  21. /**
  22. * Generate a new CSRF token.
  23. *
  24. * @param int $length Length of the token in characters.
  25. */
  26. public function generateToken(int $length = 32): string {
  27. return $this->random->generate($length);
  28. }
  29. }