1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* antibot |
5
|
|
|
* |
6
|
|
|
* @category Jkphl |
7
|
|
|
* @package Jkphl\Antibot |
8
|
|
|
* @subpackage Jkphl\Antibot\Infrastructure\Factory |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2020 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2020 Joschi Kuphal <[email protected]> |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Jkphl\Antibot\Infrastructure\Factory; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* HMAC factory |
41
|
|
|
* |
42
|
|
|
* @package Jkphl\Antibot |
43
|
|
|
* @subpackage Jkphl\Antibot\Infrastructure\Factory |
44
|
|
|
*/ |
45
|
|
|
class HmacFactory |
46
|
|
|
{ |
47
|
|
|
/** |
48
|
|
|
* Returns a proper HMAC on a given input string and encryption key. |
49
|
|
|
* |
50
|
|
|
* @param string $input Input string to create HMAC from |
51
|
|
|
* @param string $secret Secret to prevent hmac being used in a different context |
52
|
|
|
* |
53
|
|
|
* @return string Resulting (hexadecimal) HMAC currently with a length of 40 (HMAC-SHA-1) |
54
|
|
|
*/ |
55
|
4 |
|
public static function createFromString(string $input, $secret = ''): string |
56
|
|
|
{ |
57
|
4 |
|
$hashAlgorithm = 'sha1'; |
58
|
4 |
|
if (extension_loaded('hash') |
59
|
4 |
|
&& function_exists('hash_hmac') |
60
|
4 |
|
&& function_exists('hash_algos') |
61
|
4 |
|
&& in_array($hashAlgorithm, hash_algos())) { |
62
|
4 |
|
return hash_hmac($hashAlgorithm, $input, $secret); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return static::createFromStringInternal($input, $secret, $hashAlgorithm); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Create the HMAC with internal tools |
70
|
|
|
* |
71
|
|
|
* @param string $input Input string to create HMAC from |
72
|
|
|
* @param string $secret Secret to prevent hmac being used in a different context |
73
|
|
|
* @param string $hashAlgorithm Hash algorithm |
74
|
|
|
* |
75
|
|
|
* @return string Resulting (hexadecimal) HMAC currently with a length of 40 (HMAC-SHA-1) |
76
|
|
|
*/ |
77
|
|
|
protected static function createFromStringInternal(string $input, string $secret, string $hashAlgorithm): string |
78
|
|
|
{ |
79
|
|
|
$hashBlocksize = 64; |
80
|
|
|
$opad = str_repeat(chr(92), $hashBlocksize); |
81
|
|
|
$ipad = str_repeat(chr(54), $hashBlocksize); |
82
|
|
|
if (strlen($secret) > $hashBlocksize) { |
83
|
|
|
$key = str_pad(pack('H*', call_user_func($hashAlgorithm, $secret)), $hashBlocksize, chr(0)); |
84
|
|
|
} else { |
85
|
|
|
$key = str_pad($secret, $hashBlocksize, chr(0)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return call_user_func($hashAlgorithm, |
89
|
|
|
($key ^ $opad).pack('H*', call_user_func($hashAlgorithm, (($key ^ $ipad).$input)))); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|