1. Do not store password as plain text
  2. Do not try to invent your own password security
  3. Do not ‘encrypt’ passwords
  4. Do not use MD5
  5. Do not use a single site-wide salt
  6. What you should do
  • Use a cryptographically strong hashing function like bcrypt (see PHP's crypt() function).
  • Use a random salt for each password.
  • Use a slow hashing algorithm to make brute force attacks practically impossible.
  • For bonus points, regenerate the hash every time a users logs in.
$username = 'Admin';
$password = 'gf45_gdf#4hg';// A higher "cost" is more secure but consumes more processing power
$cost = 10;// Create a random salt
$salt = strtr(base64_encode(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM)), '+', '.');// Prefix information about the hash so PHP knows how to verify it later.
// "$2a$" Means we're using the Blowfish algorithm. The following two digits are the cost parameter.
$salt = sprintf("$2a$%02d$", $cost) . $salt;// Value:
// $2a$10$eImiTXuWVxfM37uY4JANjQ==// Hash the password with the salt
$hash = crypt($password, $salt);// Value:
// $2a$10$eImiTXuWVxfM37uY4JANjOL.oTxqp7WylW7FCzx2Lc7VLmdJIddZq

In the above example we turned a reasonably strong password into a hash that we can safely store in a database. The next time the user logs in we can validate the password as follows:

$username = 'Admin';
$password = 'gf45_gdf#4hg';// For brevity, code to establish a database connection has been left out$sth = $dbh->prepare('SELECThashFROM usersWHEREusername = :usernameLIMIT 1');$sth->bindParam(':username', $username);$sth->execute();$user = $sth->fetch(PDO::FETCH_OBJ);// Hashing the password with its hash as the salt returns the same hash
if ( hash_equals($user->hash, crypt($password, $user->hash)) ) {// Ok!
}

A few additional tips to prevent user accounts from being hacked:

  • Limit the number of failed login attempts.
  • Require strong passwords.
  • Do not limit passwords to a certain length (remember, you're only storing a hash so length doesn't matter).
  • Allow special characters in passwords, there is no reason not to.

注意:hash_equals (PHP 5 >= 5.6.0) 如果你的php版本 phpversion()不够,可以尝试使用下面的代码

原文:https://alias.io/2010/01/store-passwords-safely-with-php-and-mysql/

password_compat

 This library requires PHP >= 5.3.7 OR a version that has the $2y fix backported into it (such as RedHat provides). Note that Debian's 5.3.3 version is NOT supported.

使用前,用下面代码测试当前域名是否可以用这个password_compat

<?php
require "lib/password.php";
echo "Test for functionality of compat library: " . (PasswordCompatbinarycheck() ? "Pass" : "Fail");
echo "n";

Usage

Creating Password Hashes

To create a password hash from a password, simply use the password_hash function.

$hash = password_hash($password, PASSWORD_BCRYPT);

Note that the algorithm that we chose is PASSWORD_BCRYPT. That's the current strongest algorithm supported. This is the BCRYPT crypt algorithm. It produces a 60 character hash as the result.

BCRYPT also allows for you to define a cost parameter in the options array. This allows for you to change the CPU cost of the algorithm:

$hash = password_hash($password, PASSWORD_BCRYPT, array("cost" => 10));

That's the same as the default. The cost can range from 4 to 31. I would suggest that you use the highest cost that you can, while keeping response time reasonable (I target between 0.1 and 0.5 seconds for a hash, depending on use-case).

Another algorithm name is supported:

    PASSWORD_DEFAULT

This will use the strongest algorithm available to PHP at the current time. Presently, this is the same as specifying PASSWORD_BCRYPT. But in future versions of PHP, it may be updated to use a stronger algorithm if one is introduced. It can also be changed if a problem is identified with the BCRYPT algorithm. Note that if you use this option, you are strongly encouraged to store it in a VARCHAR(255) column to avoid truncation issues if a future algorithm increases the length of the generated hash.

It is very important that you should check the return value of password_hash prior to storing it, because a false may be returned if it encountered an error.

Verifying Password Hashes

To verify a hash created by password_hash, simply call:

    if (password_verify($password, $hash)) {/* Valid */} else {/* Invalid */}

That's all there is to it.

Rehashing Passwords

From time to time you may update your hashing parameters (algorithm, cost, etc). So a function to determine if rehashing is necessary is available:

    if (password_verify($password, $hash)) {if (password_needs_rehash($hash, $algorithm, $options)) {$hash = password_hash($password, $algorithm, $options);/* Store new hash in db */}}

项目地址:https://github.com/ircmaxell/password_compat

下载:password_compat-master

转自:PHP 加密用户密码 How to store passwords safely with PHP and MySQL

PHP 加密用户密码 How to store passwords safely with PHP and MySQL相关推荐

  1. 用户密码加密存储十问十答,一文说透密码安全存储

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 作者 | 程序员赵鑫 来源 | cnblogs.com/xinzh ...

  2. C#中使用MD5对用户密码加密与解密

    C#中常涉及到对用户密码的加密于解密的算法,其中使用MD5加密是最常见的的实现方式.本文总结了通用的算法并结合了自己的一点小经验,分享给大家. 一.使用16位.32位.64位MD5方法对用户名加密 1 ...

  3. [转]常见的用户密码加密方式以及破解方法

    [作者]张辉,就职于携程技术中心信息安全部,负责安全产品的设计与研发. 作为互联网公司的信息安全从业人员经常要处理撞库扫号事件,产生撞库扫号的根本原因是一些企业发生了信息泄露事件,且这些泄露数据未加密 ...

  4. C#对用户密码使用MD5加密与解密

    C#中常涉及到对用户密码的加密于解密的算法,其中使用MD5加密是最常见的的实现方式.本文总结了通用的算法并结合了自己的一点小经验,分享给大家. 一.使用16位.32位.64位MD5方法对用户名加密 1 ...

  5. 如何正确对用户密码进行加密?转自https://blog.csdn.net/zhouyan8603/article/details/80473083...

    本文介绍了对密码哈希加密的基础知识,以及什么是正确的加密方式.还介绍了常见的密码破解方法,给出了如何避免密码被破解的思路.相信读者阅读本文后,就会对密码的加密有一个正确的认识,并对密码正确进行加密措施 ...

  6. md5加密数据表中的密码php,JSP_使用MD5加密数据库中的用户密码(一),我们知道,现在网络上一般的 - phpStudy...

    使用MD5加密数据库中的用户密码(一) 我们知道,现在网络上一般的网站,稍微完善一点的,往往都需要用户先注册,提供诸如电子邮件.账号.密码等信息以后,成为网站栏目的注册用户,才可以享受网站一些特殊栏目 ...

  7. 如何正确对用户密码进行加密?

    本文介绍了对密码哈希加密的基础知识,以及什么是正确的加密方式.还介绍了常见的密码破解方法,给出了如何避免密码被破解的思路.相信读者阅读本文后,就会对密码的加密有一个正确的认识,并对密码正确进行加密措施 ...

  8. 使用MD5对用户密码加密与解密

    MD5简介 : MD5的全称是Message-Digest Algorithm 5,在90年代初由MIT的计算机科学实验室和RSA Data Security Inc发明,经MD2.MD3和MD4发展 ...

  9. 使用MD5加密数据库中的用户密码介绍

    使用MD5加密数据库中的用户密码介绍 ●前言 我们知道,现在网络上一般的网站,稍微完善一点的,往往都需要用户先注册,提供诸如电子邮件.账号.密码等信息以后,成为网站栏目的注册用户,才可以享受网站一些特 ...

最新文章

  1. 用友 传入的 json 格式无效_用友网络股吧:被错杀的半导体材料龙头,全年或60%高增长,刚刚走出黄金坑...
  2. 软件开发管理规范流程图
  3. 基于mysql传统复制模式转为GTID模式 mysql 5.7版本
  4. eclipse——jsp字体设置
  5. 卷积神经网络中不同类型的卷积方式介绍
  6. 口语语言理解(SLU)最新资源库:综述、数据集、开源论文
  7. Linux下多窗口分屏式终端--Terminator
  8. razor 写入html标记,如何在Razor中编写“ Html.BeginForm”
  9. wordList04
  10. 网络请求数据解析时,判断数据是否为空
  11. 市场活动课件:SQL Server 索引优化
  12. 中小企业上云首选,华为云全新云服务器S6性能评测分析
  13. 在.NET环境中实现每日构建(Daily Build)--ccnet,MSBuild篇
  14. Open3d之点云上色
  15. Shell定时删除日志
  16. python3将seq文件转化为avi
  17. 没有实际的工作经验,如何面试Linux运维工程师?
  18. 关于SoUI界面库处理从WM_LBUTTONUP到按钮事件的消息路由过程分析
  19. html怎么把桌面的图片放大缩小,css怎么让图片随屏幕大小改变?
  20. ansible模块authorized_key

热门文章

  1. 计算字符串距离(信息学奥赛一本通-T1298)
  2. 高精度减法(洛谷-P2142)
  3. 信息学奥赛C++语言:滔滔吃苹果
  4. 信息学奥赛一本通C++语言——1083:计算星期几
  5. 信息学奥赛一本通C++语言——1069:乘方计算
  6. 4 SD配置-企业结构-定义-定义销售办公室
  7. 23 OBYC配置COC报错-过账码 未定义
  8. 8 SAP QUERY定制报表操作手册 SQVI-推荐
  9. Html转快应用方法,卡片跳转快应用指定页面,如何点返回直接退出快应用回到卡片...
  10. mysql查询索引位置_mysql索引在什么位置