Skip to content

Commit 930fa6c

Browse files
TimWollanicolas-grekas
authored andcommitted
Polyfill the random extension's interfaces and Secure engine
1 parent 011cd09 commit 930fa6c

File tree

10 files changed

+148
-0
lines changed

10 files changed

+148
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ Polyfills are provided for:
6565
- the `AllowDynamicProperties` attribute introduced in PHP 8.2;
6666
- the `SensitiveParameter` attribute introduced in PHP 8.2;
6767
- the `SensitiveParameterValue` class introduced in PHP 8.2;
68+
- the `Random\Engine` interface introduced in PHP 8.2;
69+
- the `Random\CryptoSafeEngine` interface introduced in PHP 8.2;
70+
- the `Random\Engine\Secure` class introduced in PHP 8.2;
6871
- the `json_validate` function introduced in PHP 8.3;
6972

7073
It is strongly recommended to upgrade your PHP version and/or install the missing

src/Php82/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ This component provides features added to PHP 8.2 core:
66
- [`AllowDynamicProperties`](https://wiki.php.net/rfc/deprecate_dynamic_properties)
77
- [`SensitiveParameter`](https://wiki.php.net/rfc/redact_parameters_in_back_traces)
88
- [`SensitiveParameterValue`](https://wiki.php.net/rfc/redact_parameters_in_back_traces)
9+
- [`Random\Engine`](https://wiki.php.net/rfc/rng_extension)
10+
- [`Random\Engine\CryptoSafeEngine`](https://wiki.php.net/rfc/rng_extension)
11+
- [`Random\Engine\Secure`](https://wiki.php.net/rfc/rng_extension)
912

1013
More information can be found in the
1114
[main Polyfill README](https://github.com/symfony/polyfill/blob/main/README.md).

src/Php82/Random/Engine/Secure.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Polyfill\Php82\Random\Engine;
13+
14+
/**
15+
* @author Tim Düsterhus <[email protected]>
16+
*
17+
* @internal
18+
*/
19+
class Secure
20+
{
21+
public function __construct()
22+
{
23+
}
24+
25+
public function generate(): string
26+
{
27+
return \random_bytes(\PHP_INT_SIZE);
28+
}
29+
30+
public function __sleep(): array
31+
{
32+
throw new \Exception("Serialization of 'Random\\Engine\\Secure' is not allowed");
33+
}
34+
35+
public function __wakeup(): void
36+
{
37+
throw new \Exception("Unserialization of 'Random\\Engine\\Secure' is not allowed");
38+
}
39+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Random;
4+
5+
if (\PHP_VERSION_ID < 80200) {
6+
class BrokenRandomEngineError extends RandomError
7+
{
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Random;
4+
5+
if (\PHP_VERSION_ID < 80200) {
6+
interface CryptoSafeEngine extends Engine
7+
{
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Random;
4+
5+
if (\PHP_VERSION_ID < 80200) {
6+
interface Engine
7+
{
8+
public function generate(): string;
9+
}
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Random\Engine;
4+
5+
use \Symfony\Polyfill\Php82 as p;
6+
7+
if (\PHP_VERSION_ID < 80200) {
8+
final class Secure extends p\Random\Engine\Secure implements \Random\CryptoSafeEngine
9+
{
10+
}
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Random;
4+
5+
if (\PHP_VERSION_ID < 80200) {
6+
class RandomError extends \Error
7+
{
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Random;
4+
5+
if (\PHP_VERSION_ID < 80200) {
6+
class RandomException extends \Exception
7+
{
8+
}
9+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Polyfill\Tests\Php82;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Polyfill\Php82\Random\Engine\Secure as SecureEnginePolyfill;
16+
17+
/**
18+
* @author Tim Düsterhus <[email protected]>
19+
*/
20+
class RandomSecureEngineTest extends TestCase
21+
{
22+
public function secureEngineProvider()
23+
{
24+
yield [new SecureEnginePolyfill()];
25+
yield [new \Random\Engine\Secure()];
26+
}
27+
28+
/**
29+
* @dataProvider secureEngineProvider
30+
*/
31+
public function testGenerateLength($v)
32+
{
33+
$this->assertSame(\PHP_INT_SIZE, strlen($v->generate()));
34+
}
35+
36+
/**
37+
* @dataProvider secureEngineProvider
38+
*/
39+
public function testSerializeIsNotAllowed($v)
40+
{
41+
$this->expectException(\Exception::class);
42+
$this->expectExceptionMessage("Serialization of 'Random\\Engine\\Secure' is not allowed");
43+
44+
serialize($v);
45+
}
46+
}

0 commit comments

Comments
 (0)