<?php
/**
* Shopware 5
* Copyright (c) shopware AG
*
* According to our dual licensing model, this program can be used either
* under the terms of the GNU Affero General Public License, version 3,
* or under a proprietary license.
*
* The texts of the GNU Affero General Public License with an additional
* permission and of our proprietary license can be found at and
* in the LICENSE file you have received along with this program.
*
* 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.
*
* "Shopware" is a registered trademark of shopware AG.
* The licensing of the program under the AGPLv3 does not imply a
* trademark license. Therefore any rights, title and interest in
* our trademarks remain entirely with us.
*/
namespace Shopware\Components;
/**
* Pseudorandom number generator (PRNG).
*
* This class is highly based on Rand.php of Component_ZendMath
*
* @category Shopware
* @package Shopware\Components
*
* @link https://github.com/zendframework/zf2/blob/master/library/Zend/Math/Rand.php
* @link https://github.com/ircmaxell/RandomLib
*
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://opensource.org/licenses/bsd-license.php New BSD License
*/
abstract class Random
{
/**
* Generate random bytes using OpenSSL, Mcrypt, /dev/urandom and mt_rand() as fallback
*
* @param integer $length
* @param bool $strong If true, an exception is thrown if no secure random generator is available
* @return string
* @throws \Exception
*/
public static function getBytes($length, $strong = false)
{
if ($length <= 0) {
return false;
}
return random_bytes($length);
}
/**
* Generate random boolean
*
* @param bool $strong true if you need a strong random generator (cryptography)
* @return bool
*/
public static function getBoolean($strong = false)
{
$byte = static::getBytes(1, $strong);
return (bool) (ord($byte) % 2);
}
/**
* Generate a random integer between $min and $max inclusive
*
* @param integer $min
* @param integer $max
* @param bool $strong true if you need a strong random generator (cryptography)
* @return integer
* @throws \DomainException
*/
public static function getInteger($min, $max, $strong = false)
{
if ($min > $max) {
throw new \DomainException(
'The min parameter must be lower than max parameter'
);
}
return random_int($min, $max);
}
/**
* Generate random float (0..1)
* This function generates floats with platform-dependent precision
*
* PHP uses double precision floating-point format (64-bit) which has
* 52-bits of significand precision. We gather 7 bytes of random data,
* and we fix the exponent to the bias (1023). In this way we generate
* a float of 1.mantissa.
*
* @param bool $strong true if you need a strong random generator (cryptography)
* @return float
*/
public static function getFloat($strong = false)
{
$bytes = static::getBytes(7, $strong);
$bytes[6] = $bytes[6] | chr(0xF0);
$bytes .= chr(63); // exponent bias (1023)
list(, $float) = unpack('d', $bytes);
return ($float - 1);
}
/**
* Generate a random string of specified length.
* Prioritizes secure random generators (OpenSSL/Mcrypt)
* and uses a non-secure random generator as fallback
*
* Uses supplied character list for generating the new string.
* If no character list provided - uses Base 64 character set.
*
* @param integer $length
* @param string|null $charlist
* @param bool $strong If true, an exception is thrown if no secure random generator is available
* @return string
* @throws \DomainException
*/
public static function getString($length, $charlist = null, $strong = false)
{
if ($length < 1) {
throw new \DomainException('Length should be >= 1');
}
// charlist is empty or not provided
if (empty($charlist)) {
$numBytes = ceil($length * 0.75);
$bytes = static::getBytes($numBytes, $strong);
return substr(rtrim(base64_encode($bytes), '='), 0, $length);
}
$listLen = strlen($charlist);
if ($listLen == 1) {
return str_repeat($charlist, $length);
}
$bytes = static::getBytes($length, $strong);
$pos = 0;
$result = '';
for ($i = 0; $i < $length; $i++) {
$pos = ($pos + ord($bytes[$i])) % $listLen;
$result .= $charlist[$pos];
}
return $result;
}
/**
* Generate a random alphanumeric string of specified length.
* Prioritizes secure random generators (OpenSSL/Mcrypt)
* and uses a non-secure random generator as fallback
*
* Charlist: a-zA-Z0-9
*
* @param integer $length
* @param bool $strong If true, an exception is thrown if no secure random generator is available
* @return string
* @throws \DomainException
*/
public static function getAlphanumericString($length, $strong = false)
{
if ($length < 1) {
throw new \DomainException('Length should be >= 1');
}
$charlist = implode(range('a', 'z')) . implode(range('A', 'Z')) . implode(range(0, 9));
return static::getString($length, $charlist, $strong);
}
}
| nama | waktu | ukuran | owner | perm | aksi |
|---|---|---|---|---|---|
| .. | |||||
| Api/ | 2016-11-07 16:02 | 287.29 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Auth/ | 2016-11-07 16:02 | 9.48 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Benchmark/ | 2016-11-07 15:59 | 2.97 KB | 76129f60960:kunden | dxwrx-rx-r | |
| CategoryHandling/ | 2016-11-07 15:59 | 6.7 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Check/ | 2016-11-07 16:02 | 137.23 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Compatibility/ | 2016-11-07 15:59 | 40.29 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Console/ | 2016-11-07 15:59 | 9.29 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Convert/ | 2016-11-07 15:59 | 21.31 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Cron/ | 2016-11-07 15:59 | 1.8 KB | 76129f60960:kunden | dxwrx-rx-r | |
| DependencyInjection/ | 2016-11-07 16:02 | 89.93 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Emotion/ | 2016-11-07 15:59 | 6.4 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Escaper/ | 2016-11-07 15:59 | 5.38 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Form/ | 2016-11-07 16:02 | 45.21 KB | 76129f60960:kunden | dxwrx-rx-r | |
| HttpCache/ | 2016-11-07 15:59 | 27.91 KB | 76129f60960:kunden | dxwrx-rx-r | |
| HttpClient/ | 2016-11-07 15:59 | 261.1 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Install/ | 2016-11-07 15:59 | 3.59 KB | 76129f60960:kunden | dxwrx-rx-r | |
| LegacyRequestWrapper/ | 2016-11-07 15:59 | 9.77 KB | 76129f60960:kunden | dxwrx-rx-r | |
| License/ | 2016-11-07 16:02 | 18.86 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Log/ | 2016-11-07 16:02 | 33.09 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Migrations/ | 2016-11-07 15:59 | 10.92 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Model/ | 2016-11-07 16:04 | 141.64 KB | 76129f60960:kunden | dxwrx-rx-r | |
| MultiEdit/ | 2016-11-07 16:02 | 98 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Password/ | 2016-11-07 16:02 | 18.5 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Plugin/ | 2016-11-07 16:02 | 119.2 KB | 76129f60960:kunden | dxwrx-rx-r | |
| ProductStream/ | 2016-11-07 15:59 | 19.08 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Routing/ | 2016-11-07 16:02 | 58.6 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Snippet/ | 2016-11-07 16:02 | 38.98 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Test/ | 2016-11-07 16:02 | 4.02 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Theme/ | 2016-11-07 16:02 | 130.93 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Thumbnail/ | 2016-11-07 16:02 | 21.71 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Validator/ | 2016-11-07 15:59 | 3.01 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Acl.php | 2016-11-07 15:58 | 6.65 KB | 76129f60960:kunden | -xw-x----- | |
| AttributeSubscriber.php | 2016-11-07 15:58 | 4.73 KB | 76129f60960:kunden | -xw-x----- | |
| Auth.php | 2016-11-07 15:58 | 5.26 KB | 76129f60960:kunden | -xw-x----- | |
| CSRFTokenValidationException.php | 2016-11-07 15:58 | 1.08 KB | 76129f60960:kunden | -xw-x----- | |
| CSRFTokenValidator.php | 2016-11-07 15:58 | 5.52 KB | 76129f60960:kunden | -xw-x----- | |
| CSRFWhitelistAware.php | 2016-11-07 15:58 | 1.23 KB | 76129f60960:kunden | -xw-x----- | |
| CacheManager.php | 2016-11-07 15:58 | 12.56 KB | 76129f60960:kunden | -xw-x----- | |
| Config.php | 2016-11-07 15:58 | 7.9 KB | 76129f60960:kunden | -xw-x----- | |
| ConfigLoader.php | 2016-11-07 15:58 | 5.01 KB | 76129f60960:kunden | -xw-x----- | |
| ConfigWriter.php | 2016-11-07 15:58 | 4.32 KB | 76129f60960:kunden | -xw-x----- | |
| ContainerAwareEventManager.php | 2016-11-07 15:58 | 6.61 KB | 76129f60960:kunden | -xw-x----- | |
| CsvIterator.php | 2016-11-07 15:58 | 6.91 KB | 76129f60960:kunden | -xw-x----- | |
| Document.php | 2016-11-07 15:58 | 22.91 KB | 76129f60960:kunden | -xw-x----- | |
| Form.php | 2016-11-07 15:58 | 1.43 KB | 76129f60960:kunden | -xw-x----- | |
| HolidayTableUpdater.php | 2016-11-07 15:58 | 4.42 KB | 76129f60960:kunden | -xw-x----- | |
| Licence.php | 2016-11-07 15:58 | 1.25 KB | 76129f60960:kunden | -xw-x----- | |
| LicenseInstaller.php | 2016-11-07 15:58 | 3.04 KB | 76129f60960:kunden | -xw-x----- | |
| Logger.php | 2016-11-07 15:58 | 1.59 KB | 76129f60960:kunden | -xw-x----- | |
| MemoryLimit.php | 2016-11-07 15:58 | 2.04 KB | 76129f60960:kunden | -xw-x----- | |
| Modules.php | 2016-11-07 15:58 | 4.94 KB | 76129f60960:kunden | -xw-x----- | |
| NumberRangeIncrementer.php | 2016-11-07 15:58 | 2.16 KB | 76129f60960:kunden | -xw-x----- | |
| NumberRangeIncrementerInterface.php | 2016-11-07 15:58 | 1.65 KB | 76129f60960:kunden | -xw-x----- | |
| OpenSSLVerifier.php | 2016-11-07 15:58 | 2.82 KB | 76129f60960:kunden | -xw-x----- | |
| Plugin.php | 2016-11-07 15:58 | 5.31 KB | 76129f60960:kunden | -xw-x----- | |
| QueryAliasMapper.php | 2016-11-07 15:58 | 4.62 KB | 76129f60960:kunden | -xw-x----- | |
| Random.php | 2016-11-07 15:58 | 5.7 KB | 76129f60960:kunden | -xw-x----- | |
| ReflectionHelper.php | 2016-11-07 15:58 | 2.11 KB | 76129f60960:kunden | -xw-x----- | |
| SitePageMenu.php | 2016-11-07 15:58 | 5.13 KB | 76129f60960:kunden | -xw-x----- | |
| SitemapXMLRepository.php | 2016-11-07 15:58 | 10.86 KB | 76129f60960:kunden | -xw-x----- | |
| StringCompiler.php | 2016-11-07 15:58 | 4.64 KB | 76129f60960:kunden | -xw-x----- | |
| TemplateMail.php | 2016-11-07 15:58 | 7.54 KB | 76129f60960:kunden | -xw-x----- | |
| Theme.php | 2016-11-07 15:58 | 13.07 KB | 76129f60960:kunden | -xw-x----- | |
| Translation.php | 2016-11-07 15:58 | 16.25 KB | 76129f60960:kunden | -xw-x----- | |