| 1 |
<?php |
| 2 |
/** |
| 3 |
* The Algorithm class for hashing algorithms. |
| 4 |
* |
| 5 |
* @package WP_Defender\Component\Altcha\Hasher |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Component\Altcha\Hasher; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class Algorithm |
| 12 |
*/ |
| 13 |
final class Algorithm { |
| 14 |
public const SHA1 = 'SHA-1'; |
| 15 |
public const SHA256 = 'SHA-256'; |
| 16 |
public const SHA512 = 'SHA-512'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Tries to check if the given value is a valid algorithm. |
| 20 |
* |
| 21 |
* @param string|null $value The algorithm value. |
| 22 |
* |
| 23 |
* @return string|null The valid algorithm or null if invalid. |
| 24 |
*/ |
| 25 |
public static function try_from( ?string $value ): ?string { |
| 26 |
if ( null === $value ) { |
| 27 |
return null; |
| 28 |
} |
| 29 |
|
| 30 |
return self::is_valid( $value ) ? $value : null; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Checks if the given value is a valid algorithm. |
| 35 |
* |
| 36 |
* @param string $value The algorithm value. |
| 37 |
* |
| 38 |
* @return bool True if valid, false otherwise. |
| 39 |
*/ |
| 40 |
public static function is_valid( string $value ): bool { |
| 41 |
return in_array( |
| 42 |
$value, |
| 43 |
array( |
| 44 |
self::SHA1, |
| 45 |
self::SHA256, |
| 46 |
self::SHA512, |
| 47 |
), |
| 48 |
true |
| 49 |
); |
| 50 |
} |
| 51 |
} |
| 52 |
|