Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the encryption methods of laravel

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the encryption methods of laravel. It is very detailed and has certain reference value. Friends who are interested must finish reading it.

Laravel encryption methods: 1, using Hash, syntax "bcrypt ('encrypted text')" or "Hash::make ('encrypted text')"; 2, using Laravel encryption, syntax "encrypt ('encrypted text')".

The operating environment of this tutorial: windows7 system, Laravel6 version, DELL G3 computer.

Two encryption methods of laravel

Hash

Laravel's Hash provides secure Bcrypt and Argon2 hashing algorithms for storing user passwords.

Note: Bcrypt is an excellent choice for hash passwords because its "work factor" is adjustable, which means that as hardware capabilities improve, so does the time it takes to generate hashes.

Configuration

Apply the default hash driver configuration in the configuration file config/hashing.php, which currently supports two drivers: Bcrypt and Argon2.

Note: Argon2i drivers require PHP 7.2.0 or later, and Argon2id drivers require PHP 7.3.0 or later.

First kind

Bcrypt ('admin888')

The second kind

Use Illuminate\ Support\ Facades\ Hash;$pwd = Hash::make ('admin888'); / / encrypted storage

Verification method

If (Hash::check ('qwe123456', $pwd)) {/ / password match.} public function login () {$credentials = request ([' email', 'password']); if (! $token = auth (' api')-> attempt ($credentials)) {return response ()-> json (['error' = >' Unauthorized'], 401);} return $this- > respondWithToken ($token);}

Encrypt

Laravel's cipher uses OpenSSL to provide AES-256 and AES-128 encryption. It is strongly recommended to use the encryption settings that come with Laravel, and do not try to introduce your own "home-grown" encryption algorithm. All Laravel encrypted values are signed with a message authorization code (MAC) so that the underlying values cannot be modified once encrypted.

Configuration

Before using Laravel's cipher, you must set the key option to a 32-bit random string in the configuration file config/app.php. You can use the php artisan key:generate command to generate this key, which uses PHP's secure random byte generator to build the value of key. If this value is not set, all Laravel encrypted values are insecure.

Encrypt

You can use the helper function encrypt to encrypt the data, and all encrypted values are encrypted using the OpenSSL and AES-256-CBC password (cipher). In addition, all encrypted values are signed by a message authentication code (MAC) to prevent any modification to the encrypted string.

Encrypt ('password')

Encryption without serialization

Encrypted values are processed by the serialization function serialize during encryption, allowing encryption of objects and arrays. Therefore, encrypted data received by non-PHP clients needs to be deserialized by unserialize.

If you want to encrypt and decrypt data without serialization, you can use the encryptString and decryptString methods provided by the Crypt facade:

Use Illuminate\ Support\ Facades\ Crypt;$encrypted = Crypt::encryptString ('Hello world.'); $decrypted = Crypt::decryptString ($encrypted)

Decryption

You can use the helper function decrypt to decrypt encrypted data. If the value cannot be decrypted, for example, MAC is invalid, an Illuminate\ Contracts\ Encryption\ DecryptException exception will be thrown:

Use Illuminate\ Contracts\ Encryption\ DecryptException;try {$decrypted = decrypt ($encryptedValue);} catch (DecryptException $e) {/ /} above is all the contents of the article "what are the encryption methods of laravel". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report