06.10.2016 Views

laravel-5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Encryption 337<br />

19 public function storeSecret(Request $request, $id)<br />

20 {<br />

21 $user = User::findOrFail($id);<br />

22<br />

23 $user->fill([<br />

24 'secret' => Crypt::encrypt($request->secret)<br />

25 ])->save();<br />

26 }<br />

27 }<br />

Note: Encrypted values are passed through serialize during encryption, which allows for<br />

“encryption” of objects and arrays. Thus, non-PHP clients receiving encrypted values will<br />

need to unserialize the data.<br />

Decrypting A Value<br />

Of course, you may decrypt values using the decrypt method on the Crypt facade. If the value can<br />

not be properly decrypted, such as when the MAC is invalid, an Illuminate\Contracts\Encryption\DecryptExcept<br />

will be thrown:<br />

1 use Illuminate\Contracts\Encryption\DecryptException;<br />

2<br />

3 try {<br />

4 $decrypted = Crypt::decrypt($encryptedValue);<br />

5 } catch (DecryptException $e) {<br />

6 //<br />

7 }

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!