@@ -56,12 +56,6 @@ public function __construct($params = [])
5656
5757 if (empty ($ this ->key ))
5858 throw new \CodeIgniter \Encryption \EncryptionException ("OpenSSL handler configuration missing key. " );
59- if (empty ($ this ->hmac ))
60- throw new \CodeIgniter \Encryption \EncryptionException ("OpenSSL handler configuration missing HMAC control. " );
61- if (empty ($ this ->digest ))
62- throw new \CodeIgniter \Encryption \EncryptionException ("OpenSSL handler configuration missing HMAC digest. " );
63- if (empty ($ this ->base64 ))
64- throw new \CodeIgniter \Encryption \EncryptionException ("OpenSSL handler configuration missing base64 control. " );
6559
6660 $ this ->logger ->info ('OpenSSL handler initialized with cipher ' . $ this ->cipher . '. ' );
6761 }
@@ -75,24 +69,27 @@ public function __construct($params = [])
7569 public function encrypt ($ data )
7670 {
7771 // basic encryption
78- $ iv = ($ iv_size = openssl_cipher_iv_length ($ this ->cipher )) ? openssl_random_pseudo_bytes ($ iv_size ) : null ;
72+ $ iv = ($ iv_size = \ openssl_cipher_iv_length ($ this ->cipher )) ? \ openssl_random_pseudo_bytes ($ iv_size ) : null ;
7973
80- $ data = openssl_encrypt ($ data , $ this ->cipher , $ this ->secret , OPENSSL_RAW_DATA , $ iv );
74+ $ data = \ openssl_encrypt ($ data , $ this ->cipher , $ this ->secret , OPENSSL_RAW_DATA , $ iv );
8175
8276 if ($ data === false )
8377 return false ;
8478
8579 $ result = $ iv . $ data ;
8680
8781 // HMAC?
88- if ($ this ->hmac == ' hmac ' )
82+ if ( ! empty ( $ this ->digest ) )
8983 {
90- $ hmacKey = hash_hmac ($ this ->digest , $ result , $ this ->secret ,true );
84+ $ hmacKey = \ hash_hmac ($ this ->digest , $ result , $ this ->secret , true );
9185 $ result = $ hmacKey . $ result ;
9286 }
9387
94- if ($ this ->base64 == 'base64 ' )
95- $ result = base64_encode ($ result );
88+ if ( ! empty ($ this ->encoding ))
89+ if ($ this ->encoding == 'base64 ' )
90+ $ result = \base64_encode ($ result );
91+ elseif ($ this ->encoding == 'hex ' )
92+ $ result = \bin2hex ($ result );
9693
9794 return $ result ;
9895 }
@@ -107,21 +104,24 @@ public function encrypt($data)
107104 */
108105 public function decrypt ($ data )
109106 {
110- if ($ this ->base64 == 'base64 ' )
111- $ data = base64_decode ($ data );
107+ if ( ! empty ($ this ->encoding ))
108+ if ($ this ->encoding == 'base64 ' )
109+ $ data = \base64_decode ($ data );
110+ elseif ($ this ->encoding == 'hex ' )
111+ $ data = \hex2bin ($ data );
112112
113113 // HMAC?
114- if ($ this ->hmac == ' hmac ' )
114+ if ( ! empty ( $ this ->digest ) )
115115 {
116- $ hmacLength = self ::substr ($ this ->digest ,3 ) / 8 ;
117- $ hmacKey = self ::substr ($ data ,0 , $ hmacLength );
118- $ data = self ::substr ($ data ,$ hmacLength );
119- $ hmacCalc = hash_hmac ($ this ->digest , $ data , $ this ->secret ,true );
116+ $ hmacLength = self ::substr ($ this ->digest , 3 ) / 8 ;
117+ $ hmacKey = self ::substr ($ data , 0 , $ hmacLength );
118+ $ data = self ::substr ($ data , $ hmacLength );
119+ $ hmacCalc = \ hash_hmac ($ this ->digest , $ data , $ this ->secret , true );
120120 if ($ hmacKey != $ hmacCalc )
121121 throw new \CodeIgniter \Encryption \EncryptionException ("Message authentication failed. " );
122122 }
123-
124- if ($ iv_size = openssl_cipher_iv_length ($ this ->cipher ))
123+
124+ if ($ iv_size = \ openssl_cipher_iv_length ($ this ->cipher ))
125125 {
126126 $ iv = self ::substr ($ data , 0 , $ iv_size );
127127 $ data = self ::substr ($ data , $ iv_size );
@@ -131,7 +131,7 @@ public function decrypt($data)
131131 $ iv = null ;
132132 }
133133
134- return openssl_decrypt ($ data , $ this ->cipher , $ this ->secret , OPENSSL_RAW_DATA , $ iv );
134+ return \ openssl_decrypt ($ data , $ this ->cipher , $ this ->secret , OPENSSL_RAW_DATA , $ iv );
135135 }
136136
137137}
0 commit comments