@@ -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; |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | public static function encode($value): string |
16 | 16 | { |
17 | 17 | $result = json_encode($value); |
18 | - if($error = json_last_error()) { |
|
18 | + if ($error = json_last_error()) { |
|
19 | 19 | throw new JsonException(json_last_error_msg(), $error); |
20 | 20 | } |
21 | 21 | |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | public static function decode(string $json) |
33 | 33 | { |
34 | 34 | $result = json_decode($json, true); |
35 | - if($error = json_last_error()) { |
|
35 | + if ($error = json_last_error()) { |
|
36 | 36 | throw new JsonException(json_last_error_msg(), $error); |
37 | 37 | } |
38 | 38 |
@@ -14,7 +14,7 @@ discard block |
||
14 | 14 | public static function generateKeyPair(): array |
15 | 15 | { |
16 | 16 | $keyPair = openssl_pkey_new(); |
17 | - if(!$keyPair) { |
|
17 | + if (!$keyPair) { |
|
18 | 18 | throw new AsymmetricEncryptionException( |
19 | 19 | 'openssl_pkey_new() returned false', |
20 | 20 | AsymmetricEncryptionException::OPENSSL_ERROR |
@@ -23,7 +23,7 @@ discard block |
||
23 | 23 | |
24 | 24 | openssl_pkey_export($keyPair, $privateKey); |
25 | 25 | $details = openssl_pkey_get_details($keyPair); |
26 | - if(!$details) { |
|
26 | + if (!$details) { |
|
27 | 27 | throw new AsymmetricEncryptionException( |
28 | 28 | 'openssl_pkey_get_details() returned false', |
29 | 29 | AsymmetricEncryptionException::OPENSSL_ERROR |
@@ -74,7 +74,7 @@ discard block |
||
74 | 74 | static::validatePublicKey($publicKey); |
75 | 75 | openssl_public_decrypt(base64_decode($dataEncrypted), $dataDecrypted, $publicKey); |
76 | 76 | |
77 | - if($dataDecrypted === null) { |
|
77 | + if ($dataDecrypted === null) { |
|
78 | 78 | throw new AsymmetricEncryptionException( |
79 | 79 | 'cannot decrypt by public key', |
80 | 80 | AsymmetricEncryptionException::CANNOT_DECRYPT |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | static::validatePrivateKey($privateKey); |
96 | 96 | openssl_private_decrypt(base64_decode($dataEncrypted), $dataDecrypted, $privateKey); |
97 | 97 | |
98 | - if($dataDecrypted === null) { |
|
98 | + if ($dataDecrypted === null) { |
|
99 | 99 | throw new AsymmetricEncryptionException( |
100 | 100 | 'cannot decrypt by private key', |
101 | 101 | AsymmetricEncryptionException::CANNOT_DECRYPT |
@@ -135,7 +135,7 @@ discard block |
||
135 | 135 | int $algorithm = OPENSSL_ALGO_SHA256 |
136 | 136 | ): void { |
137 | 137 | static::validatePublicKey($publicKey); |
138 | - if(!openssl_verify(JsonHelper::encode($data), $signature, $publicKey, $algorithm)) { |
|
138 | + if (!openssl_verify(JsonHelper::encode($data), $signature, $publicKey, $algorithm)) { |
|
139 | 139 | throw new AsymmetricEncryptionException('wrong signature', AsymmetricEncryptionException::CANNOT_VERIFY); |
140 | 140 | } |
141 | 141 | } |
@@ -173,7 +173,7 @@ discard block |
||
173 | 173 | |
174 | 174 | $isCorrect = true; |
175 | 175 | |
176 | - if( |
|
176 | + if ( |
|
177 | 177 | $endLineBreak !== "" || |
178 | 178 | $beginString !== "-----BEGIN {$keyType} KEY-----" || |
179 | 179 | $endString !== "-----END {$keyType} KEY-----" || |
@@ -181,15 +181,15 @@ discard block |
||
181 | 181 | ) { |
182 | 182 | $isCorrect = false; |
183 | 183 | } else { |
184 | - foreach($arPublicKey as $part) { |
|
185 | - if(!preg_match('/^.{64}$/', $part)) { |
|
184 | + foreach ($arPublicKey as $part) { |
|
185 | + if (!preg_match('/^.{64}$/', $part)) { |
|
186 | 186 | $isCorrect = false; |
187 | 187 | break; |
188 | 188 | } |
189 | 189 | } |
190 | 190 | } |
191 | 191 | |
192 | - if(!$isCorrect) { |
|
192 | + if (!$isCorrect) { |
|
193 | 193 | throw new AsymmetricEncryptionException( |
194 | 194 | 'invalid key format', |
195 | 195 | AsymmetricEncryptionException::INVALID_KEY_FORMAT |
@@ -21,21 +21,21 @@ discard block |
||
21 | 21 | $data = JsonHelper::encode($data); |
22 | 22 | |
23 | 23 | $ivLen = openssl_cipher_iv_length($cipherMethod); |
24 | - if($ivLen === false) { |
|
24 | + if ($ivLen === false) { |
|
25 | 25 | throw new SymmetricEncryptionException( |
26 | 26 | 'openssl_cipher_iv_length() returned false', |
27 | 27 | SymmetricEncryptionException::OPENSSL_ERROR |
28 | 28 | ); |
29 | 29 | } |
30 | 30 | $iv = openssl_random_pseudo_bytes($ivLen); |
31 | - if(!$iv) { |
|
31 | + if (!$iv) { |
|
32 | 32 | throw new SymmetricEncryptionException( |
33 | 33 | 'openssl_random_pseudo_bytes() returned false', |
34 | 34 | SymmetricEncryptionException::OPENSSL_ERROR |
35 | 35 | ); |
36 | 36 | } |
37 | 37 | $cipherText = openssl_encrypt($data, $cipherMethod, $secretKey, OPENSSL_RAW_DATA, $iv); |
38 | - if($cipherText === false) { |
|
38 | + if ($cipherText === false) { |
|
39 | 39 | throw new SymmetricEncryptionException( |
40 | 40 | 'openssl_encrypt() returned false', |
41 | 41 | SymmetricEncryptionException::OPENSSL_ERROR |
@@ -59,19 +59,19 @@ discard block |
||
59 | 59 | |
60 | 60 | $c = base64_decode($encryptedData); |
61 | 61 | $ivLen = openssl_cipher_iv_length($cipherMethod); |
62 | - if($ivLen === false) { |
|
62 | + if ($ivLen === false) { |
|
63 | 63 | throw new SymmetricEncryptionException( |
64 | 64 | 'openssl_cipher_iv_length() returned false', |
65 | 65 | SymmetricEncryptionException::OPENSSL_ERROR |
66 | 66 | ); |
67 | 67 | } |
68 | 68 | $iv = substr($c, 0, $ivLen); |
69 | - $hmac = substr($c, $ivLen, $sha2len=32); |
|
69 | + $hmac = substr($c, $ivLen, $sha2len = 32); |
|
70 | 70 | $cipherText = substr($c, $ivLen+$sha2len); |
71 | 71 | |
72 | 72 | $data = openssl_decrypt($cipherText, $cipherMethod, $secretKey, OPENSSL_RAW_DATA, $iv); |
73 | 73 | |
74 | - if($data === false) { |
|
74 | + if ($data === false) { |
|
75 | 75 | throw new SymmetricEncryptionException( |
76 | 76 | 'incorrect secret key', |
77 | 77 | SymmetricEncryptionException::CANNOT_DECRYPT |
@@ -94,7 +94,7 @@ discard block |
||
94 | 94 | */ |
95 | 95 | public static function checkCipherMethodAvailable(string $cipherMethod): void |
96 | 96 | { |
97 | - if(!in_array($cipherMethod, static::getCipherMethodList(), true)) { |
|
97 | + if (!in_array($cipherMethod, static::getCipherMethodList(), true)) { |
|
98 | 98 | throw new SymmetricEncryptionException( |
99 | 99 | "unknown cipher method '{$cipherMethod}'", |
100 | 100 | SymmetricEncryptionException::UNKNOWN_METHOD |