PluginProbe
Sessions / 2.3.1
Sessions v2.3.1
2.1.0 2.10.0 2.11.0 2.12.0 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.2.0 2.3.0 2.3.1 2.4.0 2.4.1 2.5.0 2.6.0 2.6.1 2.6.2 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.1.0 3.1.1 All 39 releases
sessions / includes / system / class-hash.php

class-hash.php in Sessions 2.3.1, at includes/system/class-hash.php

93 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 POSessions\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 );
65 self::$sha256_available = in_array( 'sha256', self::$x_available );
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 if ( self::$sha256_available ) {
78 $result = hash( 'sha256', (string) $secret );
79 } elseif ( self::$sha1_available ) {
80 $result = hash( 'sha1', (string) $secret );
81 } else {
82 $result = hash( 'md5', (string) $secret );
83 }
84 if ( $markup ) {
85 $result = '{' . $result . '}';
86 }
87 return $result;
88 }
89
90 }
91
92 Hash::init();
93