1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
signer.php
См. документацию.
1<?php
2
3namespace Bitrix\Main\Security\Sign;
4
5use Bitrix\Main\ArgumentTypeException;
6use Bitrix\Main\Config;
7
13class Signer
14{
16 protected $algorithm = null;
17 protected $separator = '.';
19 protected $key = null;
20
26 public function __construct(SigningAlgorithm $algorithm = null)
27 {
28 if ($algorithm !== null)
29 {
30 $this->algorithm = $algorithm;
31 }
32 else
33 {
34 $this->algorithm = new HmacAlgorithm();
35 }
36 }
37
45 public function setKey($value)
46 {
47 if (!is_string($value))
48 {
49 throw new ArgumentTypeException('value', 'string');
50 }
51
52 $this->key = $value;
53 return $this;
54 }
55
61 public function getSeparator()
62 {
63 return $this->separator;
64 }
65
73 public function setSeparator($value)
74 {
75 if (!is_string($value))
76 {
77 throw new ArgumentTypeException('value', 'string');
78 }
79
80 $this->separator = $value;
81 return $this;
82 }
83
92 public function getSignature($value, $salt = null)
93 {
94 if (!is_string($value))
95 {
96 throw new ArgumentTypeException('value', 'string');
97 }
98
99 $key = $this->getKey($salt);
100 $signature = $this->algorithm->getSignature($value, $key);
101 $signature = $this->encodeSignature($signature);
102 return $signature;
103 }
104
122 public function sign($value, $salt = null)
123 {
124 if (!is_string($value))
125 {
126 throw new ArgumentTypeException('value', 'string');
127 }
128
129 $signature = $this->getSignature($value, $salt);
130 return $this->pack([$value, $signature]);
131 }
132
166 public function unsign($signedValue, $salt = null)
167 {
168 if (!is_string($signedValue))
169 {
170 throw new ArgumentTypeException('signedValue', 'string');
171 }
172
173 [$value, $signature] = $this->unpack($signedValue);
174 if (!$this->verifySignature($value, $signature, $salt))
175 {
176 throw new BadSignatureException('Signature does not match');
177 }
178
179 return $value;
180 }
181
190 public function validate($value, $signature, $salt = null)
191 {
192 if (is_string($value) && is_string($signature))
193 {
194 return $this->verifySignature($value, $signature, $salt);
195 }
196 return false;
197 }
198
207 protected function verifySignature($value, $sig, $salt = null)
208 {
209 $key = $this->getKey($salt);
210 $signature = $this->decodeSignature($sig);
211 return $this->algorithm->verify($value, $key, $signature);
212 }
213
223 protected function getKey($salt = null)
224 {
225 if ($salt !== null && !preg_match('#^[a-zA-Z0-9_.-]{3,50}$#D', $salt))
226 {
227 throw new BadSignatureException('Malformed salt, only [a-zA-Z0-9_.-]{3,50} characters are acceptable');
228 }
229
230 if ($this->key !== null)
231 {
233 }
234 else
235 {
236 $key = $this->getDefaultKey();
237 }
238
239 return $salt . $key;
240 }
241
247 protected function getDefaultKey()
248 {
249 static $defaultKey = null;
250 if ($defaultKey === null)
251 {
252 $defaultKey = Config\Option::get('main', 'signer_default_key', false);
253 if (!$defaultKey)
254 {
255 $defaultKey = hash('sha512', \Bitrix\Main\Security\Random::getString(64));
256 Config\Option::set('main', 'signer_default_key', $defaultKey);
257 }
258
260 if (isset($options["crypto_key"]))
261 {
262 $defaultKey .= $options["crypto_key"];
263 }
264 }
265
266 return $defaultKey;
267 }
268
276 public function pack(array $values)
277 {
278 return join($this->separator, $values);
279 }
280
300 public function unpack($value, $limit = 2)
301 {
302 // Some kind of optimization
303 if ($limit === 0)
304 {
305 if (!str_contains($value, $this->separator))
306 {
307 throw new BadSignatureException('Separator not found in value');
308 }
309
310 return explode($this->separator, $value);
311 }
312
313 $result = [];
314 while (--$limit > 0)
315 {
316 $pos = bxstrrpos($value, $this->separator);
317 if ($pos === false)
318 {
319 throw new BadSignatureException('Separator not found in value');
320 }
321
322 $result[] = mb_substr($value, $pos + 1);
323 $value = mb_substr($value, 0, $pos);
324 }
325 $result[] = $value;
326
327 return array_reverse($result);
328 }
329
336 protected function encodeSignature($value)
337 {
338 return bin2hex($value);
339 }
340
348 protected function decodeSignature($value)
349 {
350 if (preg_match('#[^[:xdigit:]]#', $value))
351 {
352 throw new BadSignatureException('Signature must be hexadecimal string');
353 }
354
355 // ToDo: use hex2bin instead pack for PHP > 5.4.0
356 return pack('H*', $value);
357 }
358}
static getValue($name)
Определения configuration.php:24
static get($moduleId, $name, $default="", $siteId=false)
Определения option.php:30
static set($moduleId, $name, $value="", $siteId="")
Определения option.php:261
static getString($length, $caseSensitive=false)
Определения random.php:76
encodeSignature($value)
Определения signer.php:336
setSeparator($value)
Определения signer.php:73
validate($value, $signature, $salt=null)
Определения signer.php:190
unpack($value, $limit=2)
Определения signer.php:300
getSignature($value, $salt=null)
Определения signer.php:92
sign($value, $salt=null)
Определения signer.php:122
unsign($signedValue, $salt=null)
Определения signer.php:166
setKey($value)
Определения signer.php:45
pack(array $values)
Определения signer.php:276
decodeSignature($value)
Определения signer.php:348
getKey($salt=null)
Определения signer.php:223
verifySignature($value, $sig, $salt=null)
Определения signer.php:207
__construct(SigningAlgorithm $algorithm=null)
Определения signer.php:26
$options
Определения commerceml2.php:49
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
bxstrrpos($haystack, $needle)
Определения tools.php:3292