@@ -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 |
@@ -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 |
@@ -19,7 +19,7 @@ discard block |
||
19 | 19 | public static function generateKeyPair(): array |
20 | 20 | { |
21 | 21 | $keyPair = openssl_pkey_new(); |
22 | - if(!$keyPair) { |
|
22 | + if (!$keyPair) { |
|
23 | 23 | throw new AsymmetricEncryptionException( |
24 | 24 | 'openssl_pkey_new() returned false', |
25 | 25 | AsymmetricEncryptionException::OPENSSL_ERROR |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | |
29 | 29 | openssl_pkey_export($keyPair, $privateKey); |
30 | 30 | $details = openssl_pkey_get_details($keyPair); |
31 | - if(!$details) { |
|
31 | + if (!$details) { |
|
32 | 32 | throw new AsymmetricEncryptionException( |
33 | 33 | 'openssl_pkey_get_details() returned false', |
34 | 34 | AsymmetricEncryptionException::OPENSSL_ERROR |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | public static function encryptByPublicKey($data, string $publicKey): string |
52 | 52 | { |
53 | 53 | static::validatePublicKey($publicKey); |
54 | - if(!openssl_public_encrypt(JsonHelper::encode($data), $dataEncrypted, $publicKey)) { |
|
54 | + if (!openssl_public_encrypt(JsonHelper::encode($data), $dataEncrypted, $publicKey)) { |
|
55 | 55 | throw new AsymmetricEncryptionException( |
56 | 56 | 'openssl_public_encrypt() returned false', |
57 | 57 | AsymmetricEncryptionException::CANNOT_ENCRYPT |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | public static function encryptByPrivateKey($data, string $privateKey): string |
72 | 72 | { |
73 | 73 | static::validatePrivateKey($privateKey); |
74 | - if(!openssl_private_encrypt(JsonHelper::encode($data), $dataEncrypted, $privateKey)) { |
|
74 | + if (!openssl_private_encrypt(JsonHelper::encode($data), $dataEncrypted, $privateKey)) { |
|
75 | 75 | throw new AsymmetricEncryptionException( |
76 | 76 | 'openssl_private_encrypt() returned false', |
77 | 77 | AsymmetricEncryptionException::CANNOT_ENCRYPT |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | static::validatePublicKey($publicKey); |
93 | 93 | openssl_public_decrypt(base64_decode($dataEncrypted), $dataDecrypted, $publicKey); |
94 | 94 | |
95 | - if($dataDecrypted === null) { |
|
95 | + if ($dataDecrypted === null) { |
|
96 | 96 | throw new AsymmetricEncryptionException( |
97 | 97 | 'cannot decrypt by public key', |
98 | 98 | AsymmetricEncryptionException::CANNOT_DECRYPT |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | static::validatePrivateKey($privateKey); |
115 | 115 | openssl_private_decrypt(base64_decode($dataEncrypted), $dataDecrypted, $privateKey); |
116 | 116 | |
117 | - if($dataDecrypted === null) { |
|
117 | + if ($dataDecrypted === null) { |
|
118 | 118 | throw new AsymmetricEncryptionException( |
119 | 119 | 'cannot decrypt by private key', |
120 | 120 | AsymmetricEncryptionException::CANNOT_DECRYPT |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | int $algorithm = OPENSSL_ALGO_SHA256 |
157 | 157 | ): void { |
158 | 158 | static::validatePublicKey($publicKey); |
159 | - if(!openssl_verify(JsonHelper::encode($data), $signature, $publicKey, $algorithm)) { |
|
159 | + if (!openssl_verify(JsonHelper::encode($data), $signature, $publicKey, $algorithm)) { |
|
160 | 160 | throw new AsymmetricEncryptionException('wrong signature', AsymmetricEncryptionException::CANNOT_VERIFY); |
161 | 161 | } |
162 | 162 | } |
@@ -197,7 +197,7 @@ discard block |
||
197 | 197 | |
198 | 198 | $isCorrect = true; |
199 | 199 | |
200 | - if( |
|
200 | + if ( |
|
201 | 201 | $endLineBreak !== "" || |
202 | 202 | $beginString !== "-----BEGIN {$keyType} KEY-----" || |
203 | 203 | $endString !== "-----END {$keyType} KEY-----" || |
@@ -205,15 +205,15 @@ discard block |
||
205 | 205 | ) { |
206 | 206 | $isCorrect = false; |
207 | 207 | } else { |
208 | - foreach($arPublicKey as $part) { |
|
209 | - if(!preg_match('/^.{64}$/', $part)) { |
|
208 | + foreach ($arPublicKey as $part) { |
|
209 | + if (!preg_match('/^.{64}$/', $part)) { |
|
210 | 210 | $isCorrect = false; |
211 | 211 | break; |
212 | 212 | } |
213 | 213 | } |
214 | 214 | } |
215 | 215 | |
216 | - if(!$isCorrect) { |
|
216 | + if (!$isCorrect) { |
|
217 | 217 | throw new AsymmetricEncryptionException( |
218 | 218 | 'invalid key format', |
219 | 219 | AsymmetricEncryptionException::INVALID_KEY_FORMAT |