2014-08-26 13:02:40 -04:00
|
|
|
<?php
|
2019-12-03 13:57:53 -05:00
|
|
|
|
2018-01-14 05:33:53 -05:00
|
|
|
declare(strict_types=1);
|
2014-08-26 13:02:40 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2014-08-26 13:02:40 -04:00
|
|
|
*/
|
|
|
|
|
namespace OCP\Security;
|
|
|
|
|
|
|
|
|
|
/**
|
2015-12-11 00:17:47 -05:00
|
|
|
* Class SecureRandom provides a wrapper around the random_int function to generate
|
|
|
|
|
* secure random strings. For PHP 7 the native CSPRNG is used, older versions do
|
|
|
|
|
* use a fallback.
|
2014-08-26 13:02:40 -04:00
|
|
|
*
|
|
|
|
|
* Usage:
|
2025-05-14 18:19:38 -04:00
|
|
|
* \OCP\Server::get(ISecureRandom::class)->generate(10);
|
2014-08-26 13:02:40 -04:00
|
|
|
*
|
2015-04-16 11:00:08 -04:00
|
|
|
* @since 8.0.0
|
2014-08-26 13:02:40 -04:00
|
|
|
*/
|
|
|
|
|
interface ISecureRandom {
|
2014-09-03 05:03:27 -04:00
|
|
|
/**
|
|
|
|
|
* Flags for characters that can be used for <code>generate($length, $characters)</code>
|
2024-02-14 14:48:27 -05:00
|
|
|
* @since 8.0.0
|
2014-09-03 05:03:27 -04:00
|
|
|
*/
|
2020-04-10 10:54:27 -04:00
|
|
|
public const CHAR_UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
2024-02-14 14:48:27 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @since 8.0.0
|
|
|
|
|
*/
|
2020-04-10 10:54:27 -04:00
|
|
|
public const CHAR_LOWER = 'abcdefghijklmnopqrstuvwxyz';
|
2024-02-14 14:48:27 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @since 8.0.0
|
|
|
|
|
*/
|
2020-04-10 10:54:27 -04:00
|
|
|
public const CHAR_DIGITS = '0123456789';
|
2024-02-14 14:48:27 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @since 8.0.0
|
|
|
|
|
*/
|
2020-04-10 10:54:27 -04:00
|
|
|
public const CHAR_SYMBOLS = '!\"#$%&\\\'()*+,-./:;<=>?@[\]^_`{|}~';
|
2024-02-14 14:48:27 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @since 12.0.0
|
|
|
|
|
*/
|
2021-07-07 11:52:46 -04:00
|
|
|
public const CHAR_ALPHANUMERIC = self::CHAR_UPPER . self::CHAR_LOWER . self::CHAR_DIGITS;
|
2014-09-03 05:03:27 -04:00
|
|
|
|
2017-05-07 17:10:02 -04:00
|
|
|
/**
|
|
|
|
|
* Characters that can be used for <code>generate($length, $characters)</code>, to
|
2024-02-14 14:48:27 -05:00
|
|
|
* generate human-readable random strings. Lower- and upper-case characters and digits
|
2017-05-07 17:10:02 -04:00
|
|
|
* are included. Characters which are ambiguous are excluded, such as I, l, and 1 and so on.
|
2024-02-14 14:48:27 -05:00
|
|
|
*
|
|
|
|
|
* @since 23.0.0
|
2017-05-07 17:10:02 -04:00
|
|
|
*/
|
2020-04-10 10:54:27 -04:00
|
|
|
public const CHAR_HUMAN_READABLE = 'abcdefgijkmnopqrstwxyzABCDEFGHJKLMNPQRSTWXYZ23456789';
|
2017-05-07 17:10:02 -04:00
|
|
|
|
2014-08-26 13:02:40 -04:00
|
|
|
/**
|
|
|
|
|
* Generate a random string of specified length.
|
2015-04-27 07:31:18 -04:00
|
|
|
* @param int $length The length of the generated string
|
2015-11-06 10:24:26 -05:00
|
|
|
* @param string $characters An optional list of characters to use if no character list is
|
2015-02-13 05:35:12 -05:00
|
|
|
* specified all valid base64 characters are used.
|
2014-08-26 13:02:40 -04:00
|
|
|
* @return string
|
2015-04-16 11:00:08 -04:00
|
|
|
* @since 8.0.0
|
2014-08-26 13:02:40 -04:00
|
|
|
*/
|
2018-01-13 15:39:34 -05:00
|
|
|
public function generate(int $length,
|
|
|
|
|
string $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'): string;
|
2014-08-26 13:02:40 -04:00
|
|
|
}
|