@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | $internalKeyEncrypted = AsymmetricEncryptionHelper::encryptByPublicKey($internalKey, $publicKey); |
30 | 30 | try { |
31 | 31 | $dataEncrypted = SymmetricEncryptionHelper::encrypt($data, $internalKey); |
32 | - } catch(SymmetricEncryptionException $e) { |
|
32 | + } catch (SymmetricEncryptionException $e) { |
|
33 | 33 | throw new AsymmetricEncryptionException( |
34 | 34 | 'cannot encrypt', |
35 | 35 | AsymmetricEncryptionException::CANNOT_ENCRYPT, |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | $internalKeyEncrypted = AsymmetricEncryptionHelper::encryptByPrivateKey($internalKey, $privateKey); |
54 | 54 | try { |
55 | 55 | $dataEncrypted = SymmetricEncryptionHelper::encrypt($data, $internalKey); |
56 | - } catch(SymmetricEncryptionException $e) { |
|
56 | + } catch (SymmetricEncryptionException $e) { |
|
57 | 57 | throw new AsymmetricEncryptionException( |
58 | 58 | 'cannot encrypt', |
59 | 59 | AsymmetricEncryptionException::CANNOT_ENCRYPT, |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | |
82 | 82 | try { |
83 | 83 | return SymmetricEncryptionHelper::decrypt($dataPartEncrypted, $internalKeyDecrypted); |
84 | - } catch(SymmetricEncryptionException $e) { |
|
84 | + } catch (SymmetricEncryptionException $e) { |
|
85 | 85 | throw new AsymmetricEncryptionException( |
86 | 86 | 'cannot decrypt', |
87 | 87 | AsymmetricEncryptionException::CANNOT_DECRYPT, |
@@ -107,7 +107,7 @@ discard block |
||
107 | 107 | |
108 | 108 | try { |
109 | 109 | return SymmetricEncryptionHelper::decrypt($dataPartEncrypted, $internalKeyDecrypted); |
110 | - } catch(SymmetricEncryptionException $e) { |
|
110 | + } catch (SymmetricEncryptionException $e) { |
|
111 | 111 | throw new AsymmetricEncryptionException( |
112 | 112 | 'cannot decrypt', |
113 | 113 | AsymmetricEncryptionException::CANNOT_DECRYPT, |
@@ -124,7 +124,7 @@ discard block |
||
124 | 124 | protected static function getPrefixMatches(string $dataEncrypted): array |
125 | 125 | { |
126 | 126 | preg_match('/^([0-9]+)_/', $dataEncrypted, $matches); |
127 | - if(!isset($matches[1])) { |
|
127 | + if (!isset($matches[1])) { |
|
128 | 128 | throw new AsymmetricEncryptionException('cannot decrypt', AsymmetricEncryptionException::CANNOT_DECRYPT); |
129 | 129 | } |
130 | 130 | $matches[0] = strlen($matches[0]); |
@@ -142,8 +142,8 @@ discard block |
||
142 | 142 | $charactersLength = strlen($characters); |
143 | 143 | $randomString = ''; |
144 | 144 | |
145 | - for($i = 0; $i < $length; $i++) { |
|
146 | - $randomString .= $characters[rand(0, $charactersLength - 1)]; |
|
145 | + for ($i = 0; $i < $length; $i++) { |
|
146 | + $randomString .= $characters[rand(0, $charactersLength-1)]; |
|
147 | 147 | } |
148 | 148 | |
149 | 149 | return $randomString; |
@@ -13,7 +13,7 @@ discard block |
||
13 | 13 | public static function generateKeyPair(): array |
14 | 14 | { |
15 | 15 | $keyPair = openssl_pkey_new(); |
16 | - if(!$keyPair) { |
|
16 | + if (!$keyPair) { |
|
17 | 17 | throw new AsymmetricEncryptionException( |
18 | 18 | 'openssl_pkey_new() returned false', |
19 | 19 | AsymmetricEncryptionException::OPENSSL_ERROR |
@@ -22,7 +22,7 @@ discard block |
||
22 | 22 | |
23 | 23 | openssl_pkey_export($keyPair, $privateKey); |
24 | 24 | $details = openssl_pkey_get_details($keyPair); |
25 | - if(!$details) { |
|
25 | + if (!$details) { |
|
26 | 26 | throw new AsymmetricEncryptionException( |
27 | 27 | 'openssl_pkey_get_details() returned false', |
28 | 28 | AsymmetricEncryptionException::OPENSSL_ERROR |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | static::validatePublicKey($publicKey); |
76 | 76 | openssl_public_decrypt(base64_decode($dataEncrypted), $dataDecrypted, $publicKey); |
77 | 77 | |
78 | - if($dataDecrypted === null) { |
|
78 | + if ($dataDecrypted === null) { |
|
79 | 79 | throw new AsymmetricEncryptionException( |
80 | 80 | 'cannot decrypt by public key', |
81 | 81 | AsymmetricEncryptionException::CANNOT_DECRYPT |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | static::validatePrivateKey($privateKey); |
97 | 97 | openssl_private_decrypt(base64_decode($dataEncrypted), $dataDecrypted, $privateKey); |
98 | 98 | |
99 | - if($dataDecrypted === null) { |
|
99 | + if ($dataDecrypted === null) { |
|
100 | 100 | throw new AsymmetricEncryptionException( |
101 | 101 | 'cannot decrypt by private key', |
102 | 102 | AsymmetricEncryptionException::CANNOT_DECRYPT |
@@ -138,7 +138,7 @@ discard block |
||
138 | 138 | static::validatePublicKey($publicKey); |
139 | 139 | /** @var string $jsonData */ |
140 | 140 | $jsonData = json_encode($data); // TODO throw exception if false |
141 | - if(!openssl_verify($jsonData, $signature, $publicKey, $algorithm)) { |
|
141 | + if (!openssl_verify($jsonData, $signature, $publicKey, $algorithm)) { |
|
142 | 142 | throw new AsymmetricEncryptionException('wrong signature', AsymmetricEncryptionException::CANNOT_VERIFY); |
143 | 143 | } |
144 | 144 | } |
@@ -176,7 +176,7 @@ discard block |
||
176 | 176 | |
177 | 177 | $isCorrect = true; |
178 | 178 | |
179 | - if( |
|
179 | + if ( |
|
180 | 180 | $endLineBreak !== "" || |
181 | 181 | $beginString !== "-----BEGIN {$keyType} KEY-----" || |
182 | 182 | $endString !== "-----END {$keyType} KEY-----" || |
@@ -184,15 +184,15 @@ discard block |
||
184 | 184 | ) { |
185 | 185 | $isCorrect = false; |
186 | 186 | } else { |
187 | - foreach($arPublicKey as $part) { |
|
188 | - if(!preg_match('/^.{64}$/', $part)) { |
|
187 | + foreach ($arPublicKey as $part) { |
|
188 | + if (!preg_match('/^.{64}$/', $part)) { |
|
189 | 189 | $isCorrect = false; |
190 | 190 | break; |
191 | 191 | } |
192 | 192 | } |
193 | 193 | } |
194 | 194 | |
195 | - if(!$isCorrect) { |
|
195 | + if (!$isCorrect) { |
|
196 | 196 | throw new AsymmetricEncryptionException( |
197 | 197 | 'invalid key format', |
198 | 198 | AsymmetricEncryptionException::INVALID_KEY_FORMAT |
@@ -20,21 +20,21 @@ discard block |
||
20 | 20 | $data = json_encode($data); // TODO throw exception if false |
21 | 21 | |
22 | 22 | $ivLen = openssl_cipher_iv_length($cipherMethod); |
23 | - if($ivLen === false) { |
|
23 | + if ($ivLen === false) { |
|
24 | 24 | throw new SymmetricEncryptionException( |
25 | 25 | 'openssl_cipher_iv_length() returned false', |
26 | 26 | SymmetricEncryptionException::OPENSSL_ERROR |
27 | 27 | ); |
28 | 28 | } |
29 | 29 | $iv = openssl_random_pseudo_bytes($ivLen); |
30 | - if(!$iv) { |
|
30 | + if (!$iv) { |
|
31 | 31 | throw new SymmetricEncryptionException( |
32 | 32 | 'openssl_random_pseudo_bytes() returned false', |
33 | 33 | SymmetricEncryptionException::OPENSSL_ERROR |
34 | 34 | ); |
35 | 35 | } |
36 | 36 | $cipherText = openssl_encrypt($data, $cipherMethod, $secretKey, OPENSSL_RAW_DATA, $iv); |
37 | - if($cipherText === false) { |
|
37 | + if ($cipherText === false) { |
|
38 | 38 | throw new SymmetricEncryptionException( |
39 | 39 | 'openssl_encrypt() returned false', |
40 | 40 | SymmetricEncryptionException::OPENSSL_ERROR |
@@ -58,19 +58,19 @@ discard block |
||
58 | 58 | |
59 | 59 | $c = base64_decode($encryptedData); |
60 | 60 | $ivLen = openssl_cipher_iv_length($cipherMethod); |
61 | - if($ivLen === false) { |
|
61 | + if ($ivLen === false) { |
|
62 | 62 | throw new SymmetricEncryptionException( |
63 | 63 | 'openssl_cipher_iv_length() returned false', |
64 | 64 | SymmetricEncryptionException::OPENSSL_ERROR |
65 | 65 | ); |
66 | 66 | } |
67 | 67 | $iv = substr($c, 0, $ivLen); |
68 | - $hmac = substr($c, $ivLen, $sha2len=32); |
|
68 | + $hmac = substr($c, $ivLen, $sha2len = 32); |
|
69 | 69 | $cipherText = substr($c, $ivLen+$sha2len); |
70 | 70 | |
71 | 71 | $data = openssl_decrypt($cipherText, $cipherMethod, $secretKey, OPENSSL_RAW_DATA, $iv); |
72 | 72 | |
73 | - if($data === false) { |
|
73 | + if ($data === false) { |
|
74 | 74 | throw new SymmetricEncryptionException( |
75 | 75 | 'incorrect secret key', |
76 | 76 | SymmetricEncryptionException::CANNOT_DECRYPT |
@@ -93,7 +93,7 @@ discard block |
||
93 | 93 | */ |
94 | 94 | public static function checkCipherMethodAvailable(string $cipherMethod): void |
95 | 95 | { |
96 | - if(!in_array($cipherMethod, static::getCipherMethodList(), true)) { |
|
96 | + if (!in_array($cipherMethod, static::getCipherMethodList(), true)) { |
|
97 | 97 | throw new SymmetricEncryptionException( |
98 | 98 | "unknown cipher method '{$cipherMethod}'", |
99 | 99 | SymmetricEncryptionException::UNKNOWN_METHOD |