| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Challenge_Options |
| 4 |
* |
| 5 |
* @package WP_Defender\Component\Altcha |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Component\Altcha; |
| 9 |
|
| 10 |
use WP_Defender\Component\Altcha\Hasher\Algorithm; |
| 11 |
use WP_Defender\Component\Crypt; |
| 12 |
|
| 13 |
/** |
| 14 |
* Class Challenge_Options. |
| 15 |
* |
| 16 |
* @phpstan-import-type ChallengeParams from Base_Challenge_Options |
| 17 |
*/ |
| 18 |
class Challenge_Options extends Base_Challenge_Options { |
| 19 |
private const DEFAULT_SALT_LENGTH = 12; |
| 20 |
|
| 21 |
/** |
| 22 |
* Options for creation of a new challenge with sane defaults. |
| 23 |
* |
| 24 |
* @param string $algorithm Hashing algorithm to use (`SHA-1`, `SHA-256`, `SHA-512`, default: |
| 25 |
* `SHA-256`). |
| 26 |
* @param int $max_number Maximum number for the random number generator (default: 1,000,000). |
| 27 |
* @param null|\DateTimeInterface $expires Optional expiration time for the challenge. |
| 28 |
* @param array $params Optional URL-encoded query parameters. |
| 29 |
* @param int $salt_length Length of the random salt (default: 12 bytes). |
| 30 |
*/ |
| 31 |
public function __construct( |
| 32 |
string $algorithm = Algorithm::SHA256, |
| 33 |
int $max_number = self::DEFAULT_MAX_NUMBER, |
| 34 |
?\DateTimeInterface $expires = null, |
| 35 |
array $params = array(), |
| 36 |
int $salt_length = self::DEFAULT_SALT_LENGTH |
| 37 |
) { |
| 38 |
$salt_length = max( self::DEFAULT_SALT_LENGTH, $salt_length ); |
| 39 |
|
| 40 |
parent::__construct( |
| 41 |
$algorithm, |
| 42 |
$max_number, |
| 43 |
$expires, |
| 44 |
bin2hex( Crypt::random_bytes( $salt_length ) ), |
| 45 |
Crypt::random_int( 0, $max_number ), |
| 46 |
$params |
| 47 |
); |
| 48 |
} |
| 49 |
} |
| 50 |
|