| 1 |
<?php |
| 2 |
/** |
| 3 |
* Hash handling |
| 4 |
* |
| 5 |
* Handles all hash operations and detection. |
| 6 |
* |
| 7 |
* @package System |
| 8 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Decalog\System; |
| 13 |
|
| 14 |
/** |
| 15 |
* Define the hash functionality. |
| 16 |
* |
| 17 |
* Handles all hash operations and detection. |
| 18 |
* |
| 19 |
* @package System |
| 20 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 21 |
* @since 1.0.0 |
| 22 |
*/ |
| 23 |
class Hash { |
| 24 |
|
| 25 |
/** |
| 26 |
* Algo availability. |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
* @var array $x_available Is MD5 available? |
| 30 |
*/ |
| 31 |
private static $x_available = []; |
| 32 |
|
| 33 |
/** |
| 34 |
* SHA1 availability. |
| 35 |
* |
| 36 |
* @since 1.0.0 |
| 37 |
* @var boolean $sha1_available Is SHA1 available? |
| 38 |
*/ |
| 39 |
private static $sha1_available = false; |
| 40 |
|
| 41 |
/** |
| 42 |
* SHA-256 availability. |
| 43 |
* |
| 44 |
* @since 1.0.0 |
| 45 |
* @var boolean $sha256_available Is SHA-256 available? |
| 46 |
*/ |
| 47 |
private static $sha256_available = false; |
| 48 |
|
| 49 |
/** |
| 50 |
* Initializes the class and set its properties. |
| 51 |
* |
| 52 |
* @since 1.0.0 |
| 53 |
*/ |
| 54 |
public function __construct() { |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Static initialization. |
| 59 |
* |
| 60 |
* @since 1.0.0 |
| 61 |
*/ |
| 62 |
public static function init() { |
| 63 |
self::$x_available = hash_algos(); |
| 64 |
self::$sha1_available = in_array( 'sha1', self::$x_available, true ); |
| 65 |
self::$sha256_available = in_array( 'sha256', self::$x_available, true ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Simple hashing function. |
| 70 |
* |
| 71 |
* @param string $secret String to hash. |
| 72 |
* @param boolean $markup Optional. With {}. |
| 73 |
* @return string The hashed string. |
| 74 |
* @since 1.0.0 |
| 75 |
*/ |
| 76 |
public static function simple_hash( $secret, $markup = true ) { |
| 77 |
$result = ''; |
| 78 |
if ( self::$sha256_available ) { |
| 79 |
$result = hash( 'sha256', (string) $secret ); |
| 80 |
} elseif ( self::$sha1_available ) { |
| 81 |
$result = hash( 'sha1', (string) $secret ); |
| 82 |
} else { |
| 83 |
$result = hash( 'md5', (string) $secret ); |
| 84 |
} |
| 85 |
if ( $markup ) { |
| 86 |
$result = '{' . $result . '}'; |
| 87 |
} |
| 88 |
return $result; |
| 89 |
} |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
Hash::init(); |
| 94 |
|