Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
cipher.php
1<?php
9
10class Cipher
11{
13 protected $hashAlgorithm;
14 protected $ivLength;
15 protected $calculateHash;
16
24 public function __construct($cipherAlgorithm = 'aes-256-ctr', $hashAlgorithm = 'sha256', $calculateHash = true)
25 {
26 if(!function_exists('openssl_get_cipher_methods'))
27 {
28 throw new SecurityException("Openssl extension is not available.");
29 }
30 if(!in_array($cipherAlgorithm, openssl_get_cipher_methods(true)))
31 {
32 throw new SecurityException("Unknown cipher algorithm {$cipherAlgorithm}.");
33 }
34 if(!in_array($hashAlgorithm, openssl_get_md_methods(true)))
35 {
36 throw new SecurityException("Unknown hash algorithm {$hashAlgorithm}.");
37 }
38
39 $this->cipherAlgorithm = $cipherAlgorithm;
40 $this->hashAlgorithm = $hashAlgorithm;
41 $this->ivLength = openssl_cipher_iv_length($cipherAlgorithm);
42 $this->calculateHash = (bool)$calculateHash;
43 }
44
52 public function encrypt($data, $key)
53 {
54 // Initialisation vector: it MUST be different every time
55 $iv = openssl_random_pseudo_bytes($this->ivLength, $strong);
56 if(!$strong)
57 {
58 throw new SecurityException("Not a strong initialisation vector.");
59 }
60
61 // Hash the key: we shouldn't use the password itself, it can be weak
62 $keyHash = openssl_digest($iv.$key, $this->hashAlgorithm, true);
63
64 if($this->calculateHash)
65 {
66 //store the hash to check on reading
67 $dataHash = openssl_digest($data, $this->hashAlgorithm, true);
68 $data = $dataHash.$data;
69 }
70
71 // Encrypt the data
72 $encrypted = openssl_encrypt($data, $this->cipherAlgorithm, $keyHash, OPENSSL_RAW_DATA, $iv);
73 if($encrypted === false)
74 {
75 throw new SecurityException("Encryption failed: ".openssl_error_string());
76 }
77
78 // Store IV with encrypted data to use it for decryption
79 $res = $iv.$encrypted;
80
81 return $res;
82 }
83
91 public function decrypt($data, $key)
92 {
93 // Extract the initialisation vector and encrypted data
94 $iv = substr($data, 0, $this->ivLength);
95 $raw = substr($data, $this->ivLength);
96
97 // Hash the key
98 $keyHash = openssl_digest($iv.$key, $this->hashAlgorithm, true);
99
100 // Decrypt
101 $result = openssl_decrypt($raw, $this->cipherAlgorithm, $keyHash, OPENSSL_RAW_DATA, $iv);
102 if($result === false)
103 {
104 throw new SecurityException("Decryption failed: ".openssl_error_string());
105 }
106
107 if($this->calculateHash)
108 {
109 //extract the hash and decrypted data
110 $length = strlen($keyHash);
111 $hash = substr($result, 0, $length);
112 $result = substr($result, $length);
113
114 //check the hash: may be the crypto key has changed? It shouldn't.
115 $dataHash = openssl_digest($result, $this->hashAlgorithm, true);
116 if($dataHash !== $hash)
117 {
118 throw new SecurityException("The hash is incorrect: the data was corrupted or a wrong key was supplied.");
119 }
120 }
121 return $result;
122 }
123}
__construct($cipherAlgorithm='aes-256-ctr', $hashAlgorithm='sha256', $calculateHash=true)
Definition cipher.php:24