Home  >  Article  >  Backend Development  >  Questions about php registration and password generation

Questions about php registration and password generation

WBOY
WBOYOriginal
2016-09-05 08:59:531471browse

Why do we usually add a salt field when building a user table, and then when calculating the password, MD5 encryption is performed based on the password entered on the registration page and the randomly generated salt value. What are the benefits of doing this? Can anyone give me some advice? detailed explanation.

Reply content:

Why do we usually add a salt field when building a user table, and then when calculating the password, MD5 encryption is performed based on the password entered on the registration page and the randomly generated salt value. What are the benefits of doing this? Can anyone give me some advice? detailed explanation.

Weak passwords encrypted by MD5 can still be cracked through brute force or rainbow tables. Salting is added to increase the difficulty of MD5 cracking and thereby improve security.

Mainly to avoid directly obtaining the user’s password plaintext after the database is stolen.

PHP password hashing security
Password hashing related functions:
password_hash (recommended) crypt (blowfish) hash sha1 md5
hash function supports multiple hashing algorithms hash_algos, such as sha512.

PHP 5.5 has provided a native password hashing API (password_hash/password_verify),
It provides a secure way to complete password hashing and verification.
PHP 5.3.7 and subsequent versions provide a pure PHP native Compatibility library for password hashing API.
When the PHP version does not support password_hash/password_verify, use crypt implementation instead.
If you use the crypt() function for password verification, then you need to choose a string comparison algorithm that takes constant time To avoid timing attacks.
Constant time consumption means that the time consumed by string comparison is constant and does not change with the amount of input data.
The == and === operators and strcmp() in PHP None of the functions are constant string comparisons,
but password_verify() can do the job for you.
So we encourage you to use the native password hashing API whenever possible.

"Salt" in the field of encryption and decryption refers to some data added during the hashing process.
It is used to avoid comparing the output data from the calculated hash value table (called the "rainbow table"). The risk of obtaining plaintext passwords.
Simply put, "salt" is a small amount of data added to make the hash value more difficult to crack.
There are many online services that can provide the calculated hash value and its corresponding original The input list, and the amount of data is extremely large.
By adding "salt", you can avoid the risk of finding the corresponding plaintext directly from the list.
The rainbow table is a pre-calculated table for the inverse operation of the cryptographic hash function. Commonly used to crack encrypted password hashes.

The password_hash() function will randomly generate a "salt".
When using the password_hash() or crypt() function, the "salt" will be returned as part of the generated hash value.
You can store the complete return value directly to the database, because this return value already contains enough information.
Can be used directly in the password_verify() or crypt() function for password verification.

Another use of salt is that it can be used to make cookies to identify users.
For example, implementing a customized cookie session mechanism based on the database:
This cookie must not only authenticate the user, but also cannot be forged and cracked. .

<code>//保护用户密码的盐
$salt = sha1( uniqid(getmypid().'_'.mt_rand().'_', true) );

//数据库保存的用户密码($pwd_user是用户输入的密码明文)
$pwd_db = sha1($salt.sha1($pwd_user));

//cookie里的盐
//其中$global_salt是配置里定义的全局盐,用来保护用户的盐,一旦修改,所有用户的cookie都将失效.
$cookie_salt = sha1($global_salt.sha1($salt));

//最终生成的cookie内容
$cookie = base64_encode($user_id.'|'.$cookie_salt);
//如果你需要高安全性,还可以使用MCRYPT_BLOWFISH对整个cookie的内容做一次加密.
$cookie = mcrypt_blowfish($cookie, $key);

//设置cookie,这里把过期时间设为604800秒(60*60*24*7,一周)
setcookie('sessid', $cookie, time()+604800, '/', '', false, true);

//解密cookie
$cookie = mdecrypt_blowfish($_COOKIE['sessid'], $key);

//解码分割后拿到里面的$user_id和$cookie_salt
$cookie = explode('|', base64_decode($_COOKIE['sessid']));
list($user_id, $cookie_salt) = $cookie;</code>

Use bcrypt, PHP 5.5.0 has the default algorithm.
Why return md5?

http://zhuanlan.zhihu.com/p/2...

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn