1414namespace CodeIgniter \Encryption \Handlers ;
1515
1616use CodeIgniter \Encryption \Exceptions \EncryptionException ;
17+ use phpDocumentor \Reflection \PseudoTypes \NonEmptyString ;
1718use SensitiveParameter ;
1819
1920/**
@@ -34,7 +35,7 @@ class SodiumHandler extends BaseHandler
3435 /**
3536 * List of previous keys for fallback decryption.
3637 */
37- protected string $ previousKeys = '' ;
38+ protected string | array $ previousKeys = '' ;
3839
3940 /**
4041 * Block size for padding message.
@@ -85,34 +86,40 @@ public function decrypt($data, #[SensitiveParameter] $params = null)
8586 throw EncryptionException::forNeedsStarterKey ();
8687 }
8788
88- $ result = false ;
89+ // Only use fallback keys if no custom key was provided in params
90+ $ useFallback = !isset ($ params ['key ' ]);
91+
92+ $ attemptDecrypt = function ($ key ) use ($ data ) {
93+ try {
94+ $ result = $ this ->decryptWithKey ($ data , $ key );
95+ sodium_memzero ($ key );
96+ return ['success ' => true , 'data ' => $ result ];
97+ } catch (EncryptionException $ e ) {
98+ sodium_memzero ($ key );
99+ return ['success ' => false , 'exception ' => $ e ];
100+ }
101+ };
102+
103+ $ result = $ attemptDecrypt ($ this ->key );
89104
90- try {
91- $ result = $ this ->decryptWithKey ($ data , $ this ->key );
92- sodium_memzero ($ this ->key );
93- } catch (EncryptionException $ e ) {
94- $ exception = $ e ;
95- sodium_memzero ($ this ->key );
105+ if ($ result ['success ' ]) {
106+ return $ result ['data ' ];
96107 }
97108
98- if ( $ result === false && $ this -> previousKeys !== '' ) {
99- foreach ( explode ( ' , ' , $ this -> previousKeys ) as $ previousKey ) {
100- try {
101- $ result = $ this ->decryptWithKey ( $ data , $ previousKey );
102- if ( isset ( $ result ) ) {
103- return $ result ;
104- }
105- } catch ( EncryptionException ) {
106- // Try next key
109+ $ originalException = $ result [ ' exception ' ];
110+
111+ // If primary key failed and fallback is allowed, try previous keys
112+ if ( $ useFallback && ! empty ( $ this ->previousKeys )) {
113+ foreach ( $ this -> previousKeys as $ previousKey ) {
114+ $ fallbackResult = $ attemptDecrypt ( $ previousKey ) ;
115+
116+ if ( $ fallbackResult [ ' success ' ] ) {
117+ return $ fallbackResult [ ' data ' ];
107118 }
108119 }
109120 }
110121
111- if (isset ($ exception )) {
112- throw $ exception ;
113- }
114-
115- return $ result ;
122+ throw $ originalException ;
116123 }
117124
118125 /**
0 commit comments