@@ -58,9 +58,7 @@ public function encrypt($value)
5858 // Once we have the encrypted value we will go ahead base64_encode the input
5959 // vector and create the MAC for the encrypted value so we can verify its
6060 // authenticity. Then, we'll JSON encode the data in a "payload" array.
61- $ iv = base64_encode ($ iv );
62-
63- $ mac = $ this ->hash ($ value );
61+ $ mac = $ this ->hash ($ iv = base64_encode ($ iv ), $ value );
6462
6563 return base64_encode (json_encode (compact ('iv ' , 'value ' , 'mac ' )));
6664 }
@@ -126,12 +124,12 @@ protected function getJsonPayload($payload)
126124 // to decrypt the given value. We'll also check the MAC for this encryption.
127125 if ( ! $ payload or $ this ->invalidPayload ($ payload ))
128126 {
129- throw new DecryptException ("Invalid data passed to encrypter . " );
127+ throw new DecryptException ("Invalid data. " );
130128 }
131129
132- if ($ payload ['mac ' ] != $ this ->hash ($ payload ['value ' ]))
130+ if ($ payload ['mac ' ] !== $ this ->hash ($ payload [ ' iv ' ], $ payload ['value ' ]))
133131 {
134- throw new DecryptException ("MAC for payload is invalid. " );
132+ throw new DecryptException ("MAC is invalid. " );
135133 }
136134
137135 return $ payload ;
@@ -140,12 +138,13 @@ protected function getJsonPayload($payload)
140138 /**
141139 * Create a MAC for the given value.
142140 *
141+ * @param stirng $iv
143142 * @param string $value
144143 * @return string
145144 */
146- protected function hash ($ value )
145+ protected function hash ($ iv , $ value )
147146 {
148- return hash_hmac ('sha256 ' , $ value , $ this ->key );
147+ return hash_hmac ('sha256 ' , $ iv . $ value , $ this ->key );
149148 }
150149
151150 /**
0 commit comments