Array_To_CSV.php
3 years ago
Date_Format.php
3 years ago
Exact_Range.php
3 years ago
Number_Formatter.php
3 years ago
Relative_Range.php
3 years ago
Request.php
3 years ago
Salt.php
3 years ago
Security.php
3 years ago
Singleton.php
3 years ago
String_Util.php
3 years ago
Timezone.php
3 years ago
URL.php
3 years ago
WP_Async_Request.php
3 years ago
Salt.php
49 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Utils; |
| 4 | |
| 5 | class Salt |
| 6 | { |
| 7 | /* |
| 8 | * Used for salting visitor hashes. |
| 9 | */ |
| 10 | public static function visitor_token_salt(): string |
| 11 | { |
| 12 | return self::get_salt_option('iawp_salt'); |
| 13 | } |
| 14 | |
| 15 | /* |
| 16 | * Primarily used for salting request payloads. |
| 17 | */ |
| 18 | public static function request_payload_salt(): string |
| 19 | { |
| 20 | return self::get_salt_option('iawp_request_payload_salt'); |
| 21 | } |
| 22 | |
| 23 | private static function get_salt_option($name): string |
| 24 | { |
| 25 | $salt = get_option($name); |
| 26 | |
| 27 | if ($salt == false) { |
| 28 | $salt = self::generate_salt(); |
| 29 | update_option($name, $salt); |
| 30 | } |
| 31 | |
| 32 | return $salt; |
| 33 | } |
| 34 | |
| 35 | private static function generate_salt(): string |
| 36 | { |
| 37 | $length = 32; |
| 38 | |
| 39 | if (function_exists('random_bytes')) { |
| 40 | $bytes = bin2hex(random_bytes($length)); |
| 41 | } |
| 42 | if (function_exists('openssl_random_pseudo_bytes')) { |
| 43 | $bytes = bin2hex(openssl_random_pseudo_bytes($length)); |
| 44 | } |
| 45 | |
| 46 | return substr(strtr(base64_encode(hex2bin($bytes)), '+', '.'), 0, 44); |
| 47 | } |
| 48 | } |
| 49 |