@@ -56,42 +56,21 @@ public function decrypt($data, Key $key)
5656 throw new DecryptionException ('Missing nonce to decrypt data ' );
5757 }
5858
59- // Use the sodium extension (PHP 7.2 native, PECL 2.x, or paragonie/sodium_compat) if able
60- if (\function_exists ('sodium_crypto_box_open ' )) {
61- try {
62- $ decrypted = sodium_crypto_box_open (
63- $ data ,
64- $ this ->nonce ,
65- sodium_crypto_box_keypair_from_secretkey_and_publickey ($ key ->getPrivate (), $ key ->getPublic ())
66- );
67-
68- if ($ decrypted === false ) {
69- throw new DecryptionException ('Malformed message or invalid MAC ' );
70- }
71- } catch (\SodiumException $ exception ) {
72- throw new DecryptionException ('Malformed message or invalid MAC ' , $ exception ->getCode (), $ exception );
73- }
74-
75- return $ decrypted ;
76- }
77-
78- // Use the libsodium extension (PECL 1.x) if able; purposefully skipping sodium_compat fallback here as that will match the above check
79- if (\extension_loaded ('libsodium ' )) {
80- $ decrypted = \Sodium \crypto_box_open (
59+ try {
60+ $ decrypted = sodium_crypto_box_open (
8161 $ data ,
8262 $ this ->nonce ,
83- \ Sodium \crypto_box_keypair_from_secretkey_and_publickey ($ key ->getPrivate (), $ key ->getPublic ())
63+ sodium_crypto_box_keypair_from_secretkey_and_publickey ($ key ->getPrivate (), $ key ->getPublic ())
8464 );
8565
8666 if ($ decrypted === false ) {
8767 throw new DecryptionException ('Malformed message or invalid MAC ' );
8868 }
89-
90- return $ decrypted ;
69+ } catch ( \ SodiumException $ exception ) {
70+ throw new DecryptionException ( ' Malformed message or invalid MAC ' , $ exception -> getCode (), $ exception ) ;
9171 }
9272
93- // Well this is awkward
94- throw new UnsupportedCipherException (static ::class);
73+ return $ decrypted ;
9574 }
9675
9776 /**
@@ -117,30 +96,15 @@ public function encrypt($data, Key $key)
11796 throw new EncryptionException ('Missing nonce to decrypt data ' );
11897 }
11998
120- // Use the sodium extension (PHP 7.2 native, PECL 2.x, or paragonie/sodium_compat) if able
121- if (\function_exists ('sodium_crypto_box ' )) {
122- try {
123- return sodium_crypto_box (
124- $ data ,
125- $ this ->nonce ,
126- sodium_crypto_box_keypair_from_secretkey_and_publickey ($ key ->getPrivate (), $ key ->getPublic ())
127- );
128- } catch (\SodiumException $ exception ) {
129- throw new EncryptionException ('Could not encrypt file. ' , $ exception ->getCode (), $ exception );
130- }
131- }
132-
133- // Use the libsodium extension (PECL 1.x) if able; purposefully skipping sodium_compat fallback here as that will match the above check
134- if (\extension_loaded ('libsodium ' )) {
135- return \Sodium \crypto_box (
99+ try {
100+ return sodium_crypto_box (
136101 $ data ,
137102 $ this ->nonce ,
138- \ Sodium \crypto_box_keypair_from_secretkey_and_publickey ($ key ->getPrivate (), $ key ->getPublic ())
103+ sodium_crypto_box_keypair_from_secretkey_and_publickey ($ key ->getPrivate (), $ key ->getPublic ())
139104 );
105+ } catch (\SodiumException $ exception ) {
106+ throw new EncryptionException ('Could not encrypt file. ' , $ exception ->getCode (), $ exception );
140107 }
141-
142- // Well this is awkward
143- throw new UnsupportedCipherException (static ::class);
144108 }
145109
146110 /**
@@ -156,28 +120,14 @@ public function encrypt($data, Key $key)
156120 */
157121 public function generateKey (array $ options = [])
158122 {
159- // Use the sodium extension (PHP 7.2 native, PECL 2.x, or paragonie/sodium_compat) if able
160- if (\function_exists ('sodium_crypto_box_keypair ' )) {
161- try {
162- // Generate the encryption key.
163- $ pair = sodium_crypto_box_keypair ();
164-
165- return new Key ('sodium ' , sodium_crypto_box_secretkey ($ pair ), sodium_crypto_box_publickey ($ pair ));
166- } catch (\SodiumException $ exception ) {
167- throw new InvalidKeyException ('Could not generate encryption key. ' , $ exception ->getCode (), $ exception );
168- }
169- }
170-
171- // Use the libsodium extension (PECL 1.x) if able; purposefully skipping sodium_compat fallback here as that will match the above check
172- if (\extension_loaded ('libsodium ' )) {
123+ try {
173124 // Generate the encryption key.
174- $ pair = \ Sodium \crypto_box_keypair ();
125+ $ pair = sodium_crypto_box_keypair ();
175126
176- return new Key ('sodium ' , \Sodium \crypto_box_secretkey ($ pair ), \Sodium \crypto_box_publickey ($ pair ));
127+ return new Key ('sodium ' , sodium_crypto_box_secretkey ($ pair ), sodium_crypto_box_publickey ($ pair ));
128+ } catch (\SodiumException $ exception ) {
129+ throw new InvalidKeyException ('Could not generate encryption key. ' , $ exception ->getCode (), $ exception );
177130 }
178-
179- // Well this is awkward
180- throw new UnsupportedCipherException (static ::class);
181131 }
182132
183133 /**
@@ -189,8 +139,8 @@ public function generateKey(array $options = [])
189139 */
190140 public static function isSupported (): bool
191141 {
192- // Prefer ext/sodium, then ext/libsodium, then presence of paragonie/sodium_compat
193- return \function_exists ( ' sodium_crypto_box ' ) || \extension_loaded ( ' libsodium ' ) || class_exists (Compat::class) ;
142+ // Part of PHP since 7.2
143+ return true ;
194144 }
195145
196146 /**
0 commit comments