Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
cryptofield.php
1<?php
3
5
7{
8 protected $encryptionComplete = false;
9 protected $ivLength;
10
11 public function __construct($name, $parameters = array())
12 {
13 if (isset($parameters['encryption_complete']))
14 {
15 $this->encryptionComplete = !!$parameters['encryption_complete'];
16 unset($parameters['encryption_complete']);
17 }
18 if (!static::cryptoAvailable())
19 {
20 $parameters['crypto_enabled'] = false;
21 }
22 parent::__construct($name, $parameters);
23 $this->ivLength = openssl_cipher_iv_length('aes-256-ctr');
24 }
25
26 public static function cryptoAvailable($key = '')
27 {
28 if (!\Bitrix\Main\Config\Option::get("socialservices", "allow_encrypted_tokens", false))
29 return false;
30
31 return parent::cryptoAvailable($key);
32 }
33
34 public function decrypt($data)
35 {
36 if ($this->encryptionComplete)
37 return parent::decrypt($data);
38
39 if($data == '')
40 return $data;
41
42 try
43 {
44 $value = base64_decode($data);
45 if (false === $value) // not base64 decoded so not encrypted
46 return $data;
47
48 if (mb_strlen($value, 'latin1') <= $this->ivLength) // too short to be encrypted
49 return $data;
50
51 $value = static::$cipher->decrypt($value, $this->cryptoKey);
52 return $value;
53 }
54 catch(SecurityException $e)
55 {
56 return $data;
57 }
58 }
59}