Skip to content

Commit 0c50c1f

Browse files
committed
Remove unused logic in the SeureRandom class and deprecate SecureRandomInterface
1 parent 530d875 commit 0c50c1f

File tree

2 files changed

+3
-88
lines changed

2 files changed

+3
-88
lines changed

src/Symfony/Component/Security/Core/Util/SecureRandom.php

Lines changed: 1 addition & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
@trigger_error('The '.__NAMESPACE__.'\SecureRandom class is deprecated since version 2.8 and will be removed in 3.0. Use the random_bytes function instead.', E_USER_DEPRECATED);
1515

16-
use Psr\Log\LoggerInterface;
17-
1816
/**
1917
* A secure random number generator implementation.
2018
*
@@ -25,96 +23,11 @@
2523
*/
2624
final class SecureRandom implements SecureRandomInterface
2725
{
28-
private $logger;
29-
private $useOpenSsl;
30-
private $seed;
31-
private $seedUpdated;
32-
private $seedLastUpdatedAt;
33-
private $seedFile;
34-
35-
/**
36-
* Constructor.
37-
*
38-
* Be aware that a guessable seed will severely compromise the PRNG
39-
* algorithm that is employed.
40-
*
41-
* @param string $seedFile
42-
* @param LoggerInterface $logger
43-
*/
44-
public function __construct($seedFile = null, LoggerInterface $logger = null)
45-
{
46-
$this->seedFile = $seedFile;
47-
$this->logger = $logger;
48-
49-
// determine whether to use OpenSSL
50-
if (!function_exists('random_bytes') && !function_exists('openssl_random_pseudo_bytes')) {
51-
if (null !== $this->logger) {
52-
$this->logger->notice('It is recommended that you install the "paragonie/random_compat" library or enable the "openssl" extension for random number generation.');
53-
}
54-
$this->useOpenSsl = false;
55-
} else {
56-
$this->useOpenSsl = true;
57-
}
58-
}
59-
6026
/**
6127
* {@inheritdoc}
6228
*/
6329
public function nextBytes($nbBytes)
6430
{
65-
if (function_exists('random_bytes')) {
66-
return random_bytes($nbBytes);
67-
}
68-
69-
// try OpenSSL
70-
if ($this->useOpenSsl) {
71-
$bytes = openssl_random_pseudo_bytes($nbBytes, $strong);
72-
73-
if (false !== $bytes && true === $strong) {
74-
return $bytes;
75-
}
76-
77-
if (null !== $this->logger) {
78-
$this->logger->info('OpenSSL did not produce a secure random number.');
79-
}
80-
}
81-
82-
// initialize seed
83-
if (null === $this->seed) {
84-
if (null === $this->seedFile) {
85-
throw new \RuntimeException('You need to specify a file path to store the seed.');
86-
}
87-
88-
if (is_file($this->seedFile)) {
89-
list($this->seed, $this->seedLastUpdatedAt) = $this->readSeed();
90-
} else {
91-
$this->seed = uniqid(mt_rand(), true);
92-
$this->updateSeed();
93-
}
94-
}
95-
96-
$bytes = '';
97-
while (strlen($bytes) < $nbBytes) {
98-
static $incr = 1;
99-
$bytes .= hash('sha512', $incr++.$this->seed.uniqid(mt_rand(), true).$nbBytes, true);
100-
$this->seed = base64_encode(hash('sha512', $this->seed.$bytes.$nbBytes, true));
101-
$this->updateSeed();
102-
}
103-
104-
return substr($bytes, 0, $nbBytes);
105-
}
106-
107-
private function readSeed()
108-
{
109-
return json_decode(file_get_contents($this->seedFile));
110-
}
111-
112-
private function updateSeed()
113-
{
114-
if (!$this->seedUpdated && $this->seedLastUpdatedAt < time() - mt_rand(1, 10)) {
115-
file_put_contents($this->seedFile, json_encode(array($this->seed, microtime(true))));
116-
}
117-
118-
$this->seedUpdated = true;
31+
return random_bytes($nbBytes);
11932
}
12033
}

src/Symfony/Component/Security/Core/Util/SecureRandomInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* Interface that needs to be implemented by all secure random number generators.
1616
*
1717
* @author Fabien Potencier <fabien@symfony.com>
18+
*
19+
* @deprecated since version 2.8, to be removed in 3.0. Use the random_bytes function instead
1820
*/
1921
interface SecureRandomInterface
2022
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy