Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
hmacalgorithm.php
1
<?php
2
namespace
Bitrix\Main\Security\Sign
;
3
4
use
Bitrix\Main\ArgumentOutOfRangeException
;
5
use
Bitrix\Main\ArgumentTypeException
;
6
12
class
HmacAlgorithm
13
extends
SigningAlgorithm
14
{
15
// ToDo: need option here?
16
// Default hashing algorithm used by HMAC
17
protected
$hashAlgorithm
=
'sha256'
;
18
19
26
public
function
__construct
(
$hashAlgorithm
=
null
)
27
{
28
if
(
$hashAlgorithm
)
29
$this->
setHashAlgorithm
(
$hashAlgorithm
);
30
}
31
39
public
function
setHashAlgorithm
(
$hashAlgorithm
)
40
{
41
if
(!in_array(
$hashAlgorithm
, hash_algos()))
42
throw
new
ArgumentOutOfRangeException
(
'hashAlgorithm'
, hash_algos());
43
44
$this->hashAlgorithm =
$hashAlgorithm
;
45
return
$this;
46
}
47
53
public
function
getHashAlgorithm
()
54
{
55
return
$this->hashAlgorithm
;
56
}
57
65
public
function
getSignature
($value, $key)
66
{
67
return
hash_hmac($this->hashAlgorithm, $value, $key,
true
);
68
}
69
78
public
function
verify
($value, $key, $sig)
79
{
80
return
$this->
compareStrings
(
81
$this->
getSignature
($value, $key),
82
$sig
83
);
84
}
85
99
protected
function
compareStrings
($expected, $actual)
100
{
101
if
(!is_string($expected))
102
{
103
throw
new
ArgumentTypeException
(
'expected'
,
'string'
);
104
}
105
106
if
(!is_string($actual))
107
{
108
throw
new
ArgumentTypeException
(
'actual'
,
'string'
);
109
}
110
111
$lenExpected = strlen($expected);
112
$lenActual = strlen($actual);
113
114
$status = $lenExpected ^ $lenActual;
115
$len = min($lenExpected, $lenActual);
116
for
($i = 0; $i < $len; $i++)
117
{
118
$status |= ord($expected[$i]) ^ ord($actual[$i]);
119
}
120
121
return
$status === 0;
122
}
123
124
}
Bitrix\Main\ArgumentOutOfRangeException
Definition
exception.php:67
Bitrix\Main\ArgumentTypeException
Definition
exception.php:114
Bitrix\Main\Security\Sign\HmacAlgorithm
Definition
hmacalgorithm.php:14
Bitrix\Main\Security\Sign\HmacAlgorithm\__construct
__construct($hashAlgorithm=null)
Definition
hmacalgorithm.php:26
Bitrix\Main\Security\Sign\HmacAlgorithm\getSignature
getSignature($value, $key)
Definition
hmacalgorithm.php:65
Bitrix\Main\Security\Sign\HmacAlgorithm\$hashAlgorithm
$hashAlgorithm
Definition
hmacalgorithm.php:17
Bitrix\Main\Security\Sign\HmacAlgorithm\setHashAlgorithm
setHashAlgorithm($hashAlgorithm)
Definition
hmacalgorithm.php:39
Bitrix\Main\Security\Sign\HmacAlgorithm\compareStrings
compareStrings($expected, $actual)
Definition
hmacalgorithm.php:99
Bitrix\Main\Security\Sign\HmacAlgorithm\verify
verify($value, $key, $sig)
Definition
hmacalgorithm.php:78
Bitrix\Main\Security\Sign\HmacAlgorithm\getHashAlgorithm
getHashAlgorithm()
Definition
hmacalgorithm.php:53
Bitrix\Main\Security\Sign\SigningAlgorithm
Definition
signingalgorithm.php:12
Bitrix\Main\Security\Sign
Definition
badsignatureexception.php:2
modules
main
lib
security
sign
hmacalgorithm.php
Создано системой
1.10.0