Table of Contents
回复内容:
Home Backend Development PHP Tutorial 各位用php将密码存入数据库,都用什么方法进行加密的?

各位用php将密码存入数据库,都用什么方法进行加密的?

Jun 06, 2016 pm 08:45 PM
php encryption

都说md5不安全,貌似很多时候说是用sha1()进行加密更加安全,甚至还有用一种叫bcrypt的算法,各位phper平时都是怎么选择的呢?

回复内容:

都说md5不安全,貌似很多时候说是用sha1()进行加密更加安全,甚至还有用一种叫bcrypt的算法,各位phper平时都是怎么选择的呢?

题主你可以使用 md5 或者 sha1 进行初步处理,但为了更加安全,请你同时加上两个 salt,一个静态 salt,一个动态的 salt。以 md5 为例:
假设通过 POST 传来的密码为 $_POST['password'],在存入 DB 前先进行如下的操作:

<code>$password = hash('md5', $_POST['password'].$staticSalt.$dynamicSalt);
</code>
Copy after login

为了保证动态 salt 的唯一性,可以这样操作:

<code>$dynamicSalt = hash('md5', microtime());
</code>
Copy after login

对于动态的 salt 可以与生成的密码一起保存在 DB 中,而静态 salt 则可以直接放在类文件中(例如定义为一个静态属性即可)。

首先谢谢题主采纳了我的答案,但是我之前的回答并不是最佳答案,之所以有此加密的想法源于自己所读的源码可能比较老,所以并没使用上较新版本的加密方法,例如 bcrypt等。

此外,第二点,感谢评论中几位前辈的提点,已经明白设置静态 salt 的意义并不大,生成一个较长的动态 salt 已然可以解决问题。

LZ应该采用加盐HASH。
如何“腌制”密码呢?
=_,=
正确的格式应该是,用户password+动态的salt

动态的salt不能像2L所说的,使用microtime,因为时间在某些情况下不够随机,而且是可能被猜解的。
这里推荐一个我用的加盐HASH

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

解释:
首先使用mcrypt,产生电脑随机生成的,专门用户加密的随机数函数。
第二步,把得到的随机数通过base64加密,使其变长并且不利于猜解。
第三步,把得出的盐拼接到密码的后面,再对其使用sha1进行哈希
再把password存入到用户的数据库。

PS:为何不用静态的salt?没有必要,使用一个动态随机足够长的盐足矣。
为何不用MD5?因为长度不够。
为何没有使用多次HASH?因为这样反而容易发生碰撞。
HASH好之后怎么使用“腌制”好的密码?
用户注册->提交密码->产生salt->腌制好的密码存入数据库->salt存入数据库。
用户登录->提交密码->调用salt接到提交密码的后面->进行HASH->调用之前注册腌制好的密码->对比HASH值是否和这个密码相同

最后推荐一篇文章,我是从中获益的,希望LZ也有所收获。
http://blog.jobbole.com/61872/

可以试试 phpass

对密码进行哈希相对安全的方法是使用bcrypt算法。开源的phpass库以一个易于使用的类来提供此功能。

这是我以前写的一篇关于密码加密的博文,欢迎参考。
http://www.wkii.org/save-user-password-use-bcrypt-or-pbkdf2.html

另外,请参考php5.5的内置函数
http://www.php.net/manual/zh/function.hash-pbkdf2.php

我写的pbkdf2与php5.5的函数输出结果一致。

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Get the gate.io installation package for free Get the gate.io installation package for free Feb 21, 2025 pm 08:21 PM

Gate.io is a popular cryptocurrency exchange that users can use by downloading its installation package and installing it on their devices. The steps to obtain the installation package are as follows: Visit the official website of Gate.io, click "Download", select the corresponding operating system (Windows, Mac or Linux), and download the installation package to your computer. It is recommended to temporarily disable antivirus software or firewall during installation to ensure smooth installation. After completion, the user needs to create a Gate.io account to start using it.

See all articles