summaryrefslogtreecommitdiffstats
path: root/lib/private/Security/CSRF/CsrfTokenGenerator.php
blob: 9e3f15c63d26d1ca8d82310b419e977c401cb896 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php

declare(strict_types=1);
/**
 * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
 * SPDX-License-Identifier: AGPL-3.0-only
 */
namespace OC\Security\CSRF;

use OCP\Security\ISecureRandom;

/**
 * Class CsrfTokenGenerator is used to generate a cryptographically secure
 * pseudo-random number for the token.
 *
 * @package OC\Security\CSRF
 */
class CsrfTokenGenerator {
	public function __construct(
		private ISecureRandom $random,
	) {
	}

	/**
	 * Generate a new CSRF token.
	 *
	 * @param int $length Length of the token in characters.
	 */
	public function generateToken(int $length = 32): string {
		return $this->random->generate($length);
	}
}