Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
hmacalgorithm.php
1<?php
3
6
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 {
30 }
31
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 {
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}