Commit 833db337 by Larry Ullman

Touching up beginning sections

Still need to edit/complete rest.
parent a8498358
Security Security
======== ========
Good security is vital to the health and success of many websites. Unfortunately, many developers may cut corners when it comes to security due to a lack of understanding or too large of an implementation hurdle. To make your Yii-based site as secure as possible, the Yii framework has baked in several excellent, and easy to use, security features. Good security is vital to the health and success of any website. Unfortunately, many developers cut corners when it comes to security, either due to a lack of understanding or because implementation is too large of a hurdle. To make your Yii-based site as secure as possible, Yii has baked in several excellent and easy to use security features.
Hashing and verifying passwords Hashing and verifying passwords
------------------------------- -------------------------------
Most developers know that you cannot store passwords in plain text, but many believe it's safe to hash passwords using `md5` or `sha1`. There was a time when those hashing algorithms were sufficient, but modern hardware makes it possible to break those hashes very quickly using a brute force attack. Most developers know that passwords cannot be stored in plain text, but many developers believe it's still safe to hash passwords using `md5` or `sha1`. There was a time when those hashing algorithms were sufficient, but modern hardware makes it possible to break those hashes very quickly using a brute force attack.
In order to truly secure user passwords, even in the worst case scenario (your database is broken into), you need to use a hashing algorithm that is resistant to brute force attacks. The best current choice is `bcrypt`. In PHP, you can create a `bcrypt` hash by using the [crypt function](http://php.net/manual/en/function.crypt.php). However, this function is not easy to use properly, so Yii provides two helper functions to make securely generating and verifying hashes easier. In order to truly secure user passwords, even in the worst case scenario (your database is broken into), you need to use a hashing algorithm that is resistant to brute force attacks. The best current choice is `bcrypt`. In PHP, you can create a `bcrypt` hash using the [crypt function](http://php.net/manual/en/function.crypt.php). Because this function is not easy to use properly, Yii provides two helper functions to make securely generating and verifying hashes easier.
When a user provides a password for the first time (e.g., upon registration), the password needs to be hashed: When a user provides a password for the first time (e.g., upon registration), the password needs to be hashed:
...@@ -16,7 +16,7 @@ When a user provides a password for the first time (e.g., upon registration), th ...@@ -16,7 +16,7 @@ When a user provides a password for the first time (e.g., upon registration), th
$hash = \yii\helpers\Security::generatePasswordHash($password); $hash = \yii\helpers\Security::generatePasswordHash($password);
``` ```
The hash would then be associated with the model, so that it will be stored in the database for later use. The hash would then be associated with the corresponding model attribute, so that the hashed password will be stored in the database for later use.
When user attempts to log in, the submitted log in password must be verified against the previously hashed and stored password: When user attempts to log in, the submitted log in password must be verified against the previously hashed and stored password:
...@@ -34,11 +34,11 @@ Creating random data ...@@ -34,11 +34,11 @@ Creating random data
----------- -----------
Random data is useful in many cases. For example, when resetting a password via email you need to generate a token, Random data is useful in many cases. For example, when resetting a password via email you need to generate a token,
save it to database and send it via email to end user so he's able to prove that email belongs to him. It is very save it to database, and send it via email to end user, allowing him to prove that email is his. It is very
important for this token to be truly unique else there will be a possibility to predict a value and reset another user's important that this token be truly unique, else there will be a possibility to predict the token's value and reset another user's
password. password.
Yii security helper makes it as simple as: Yii security helper makes generating random data this simple:
```php ```php
$key = \yii\helpers\Security::generateRandomKey(); $key = \yii\helpers\Security::generateRandomKey();
...@@ -47,7 +47,7 @@ $key = \yii\helpers\Security::generateRandomKey(); ...@@ -47,7 +47,7 @@ $key = \yii\helpers\Security::generateRandomKey();
Encryption and decryption Encryption and decryption
------------------------- -------------------------
In order to encrypt data so only person knowing a secret passphrase or having a secret key will be able to decrypt it. In order to encrypt data so only the person knowing a secret passphrase or having a secret key will be able to decrypt it.
For example, we need to store some information in our database but we need to make sure only user knowing a secret code For example, we need to store some information in our database but we need to make sure only user knowing a secret code
can view it (even if database is leaked): can view it (even if database is leaked):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment