aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Security/SecureRandom.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/Security/SecureRandom.php')
-rw-r--r--lib/private/Security/SecureRandom.php43
1 files changed, 15 insertions, 28 deletions
diff --git a/lib/private/Security/SecureRandom.php b/lib/private/Security/SecureRandom.php
index 4bf8995d737..b2a3d19ce74 100644
--- a/lib/private/Security/SecureRandom.php
+++ b/lib/private/Security/SecureRandom.php
@@ -1,29 +1,10 @@
<?php
declare(strict_types=1);
-
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Lukas Reschke <lukas@statuscode.ch>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License, version 3,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OC\Security;
@@ -35,19 +16,25 @@ use OCP\Security\ISecureRandom;
* use a fallback.
*
* Usage:
- * \OC::$server->getSecureRandom()->generate(10);
+ * \OC::$server->get(ISecureRandom::class)->generate(10);
* @package OC\Security
*/
class SecureRandom implements ISecureRandom {
/**
- * Generate a random string of specified length.
+ * Generate a secure random string of specified length.
* @param int $length The length of the generated string
* @param string $characters An optional list of characters to use if no character list is
- * specified all valid base64 characters are used.
- * @return string
+ * specified all valid base64 characters are used.
+ * @throws \LengthException if an invalid length is requested
*/
- public function generate(int $length,
- string $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'): string {
+ public function generate(
+ int $length,
+ string $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
+ ): string {
+ if ($length <= 0) {
+ throw new \LengthException('Invalid length specified: ' . $length . ' must be bigger than 0');
+ }
+
$maxCharIndex = \strlen($characters) - 1;
$randomString = '';