How do you encrypt your password stored in the database using php?

Source: Internet
Author: User
It is said that md5 is not secure. It seems that it is more secure to use sha1 () for encryption, and even a bcrypt algorithm. How do you choose phper at ordinary times? It is said that md5 is not secure. It seems that it is more secure to use sha1 () for encryption, and even a bcrypt algorithm. How do you choose phper at ordinary times?

Reply content:

It is said that md5 is not secure. It seems that it is more secure to use sha1 () for encryption, and even a bcrypt algorithm. How do you choose phper at ordinary times?

You can usemd5Orsha1For preliminary processing, but to be more secure, please add two at the same timesalt, A staticsalt, A dynamicsalt. Tomd5For example:
Assume thatPOSTThe sent password is$_POST['password']InDBFirst, perform the following operations:

$password = hash('md5', $_POST['password'].$staticSalt.$dynamicSalt);

To ensure the dynamicsaltYou can perform the following operations:

$dynamicSalt = hash('md5', microtime());

For dynamicsaltIt can be stored together with the generated PasswordDBWhile staticsaltIt can be directly stored in a class file (for example, defined as a static property ).

First of all, thank you for choosing my answer. But my previous answer is not the best answer. The reason for this encryption is that the source code you read may be relatively old, therefore, the encryption method of the latest version is not used, for examplebcrypt.

In addition, second, I would like to thank some of my predecessors for their contributions.saltIs not significant, generate a long dynamicsaltThe problem can be solved.

LZ should use salt-based HASH.
How to "marinate" the password?
= _, =
The correct format should be: User password + dynamic salt

Dynamic salt cannot use microtime, as 2L says, because time is not random in some cases and may be guessed.
Here we recommend a salt-adding HASH

$salt=base64_encode(mcrypt_create_iv(32,MCRYPT_DEV_RANDOM));$password=sha1($register_password.$salt);

Explanation:
First, mcrypt is used to generate random number functions randomly generated by computers and encrypted by special users.
Step 2: encrypt the random number through base64 to make it longer and make it difficult to guess.
Step 3: splice the obtained salt to the end of the password, and then use sha1 to hash it.
Then, store the password to the user's database.

PS: why not use static salt? It is not necessary to use a salt that is dynamically random and long enough.
Why not use MD5? Because the length is not enough.
Why not use HASH multiple times? As a result, it is prone to collision.
After HASH, how can I use the "Pickled" password?
User Registration-> password submission-> Generate salt-> Save the pickled password to the database-> store the salt to the database.
User Login-> submit password-> call salt after receiving the password for submission-> perform hash-> register the pickled password before calling-> compare whether the HASH value is the same as the password

Finally, I recommend an article, which I will benefit from and hope LZ will gain some benefits.
Http://blog.jobbole.com/61872/

Try phpass.

The bcrypt algorithm is used to hash passwords. The open-source phpass Library provides this function with an easy-to-use class.

This is my previous blog on password encryption. Please refer to it.
Http://www.wkii.org/save-user-password-use-bcrypt-or-pbkdf2.html

For more information, see php5.5 built-in functions.
Http://www.php.net/manual/zh/function.hash-pbkdf2.php

The function output results of pbkdf2 and php5.5 are the same.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.