詳談PHP中的密碼安全性Password Hashing
如果你還在用md5加密,建議看看下方密碼加密和驗(yàn)證方式。
先看一個(gè)簡(jiǎn)單的Password Hashing例子:
<?php
//require 'password.php';
/**
* 正確的密碼是secret-password
* $passwordHash 是hash 后存儲(chǔ)的密碼
* password_verify()用于將用戶輸入的密碼和數(shù)據(jù)庫(kù)存儲(chǔ)的密碼比對(duì)。成功返回true,否則false
*/
$passwordHash = password_hash('secret-password', PASSWORD_DEFAULT);
echo $passwordHash;
if (password_verify('bad-password', $passwordHash)) {
// Correct Password
echo 'Correct Password';
} else {
echo 'Wrong password';
// Wrong password
}
下方代碼提供了一個(gè)完整的模擬的 User 類(lèi),在這個(gè)類(lèi)中,通過(guò)使用Password Hashing,既能安全地處理用戶的密碼,又能支持未來(lái)不斷變化的安全需求。
<?php
class User
{
// Store password options so that rehash & hash can share them:
const HASH = PASSWORD_DEFAULT;
const COST = 14;//可以確定該算法應(yīng)多復(fù)雜,進(jìn)而確定生成哈希值將花費(fèi)多長(zhǎng)時(shí)間。(將此值視為更改算法本身重新運(yùn)行的次數(shù),以減緩計(jì)算。)
// Internal data storage about the user:
public $data;
// Mock constructor:
public function __construct() {
// Read data from the database, storing it into $data such as:
// $data->passwordHash and $data->username
$this->data = new stdClass();
$this->data->passwordHash = 'dbd014125a4bad51db85f27279f1040a';
}
// Mock save functionality
public function save() {
// Store the data from $data back into the database
}
// Allow for changing a new password:
public function setPassword($password) {
$this->data->passwordHash = password_hash($password, self::HASH, ['cost' => self::COST]);
}
// Logic for logging a user in:
public function login($password) {
// First see if they gave the right password:
echo "Login: ", $this->data->passwordHash, "\n";
if (password_verify($password, $this->data->passwordHash)) {
// Success - Now see if their password needs rehashed
if (password_needs_rehash($this->data->passwordHash, self::HASH, ['cost' => self::COST])) {
// We need to rehash the password, and save it. Just call setPassword
$this->setPassword($password);
$this->save();
}
return true; // Or do what you need to mark the user as logged in.
}
return false;
}
}
以上這篇詳談PHP中的密碼安全性Password Hashing就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
利用discuz自帶通行證整合dedecms的方法以及文件下載
利用discuz自帶通行證整合dedecms的方法以及文件下載...2007-03-03
PHP獲取redis里不存在的6位隨機(jī)數(shù)應(yīng)用示例【設(shè)置24小時(shí)過(guò)時(shí)】
這篇文章主要介紹了PHP獲取redis里不存在的6位隨機(jī)數(shù)的方法,可設(shè)置24小時(shí)過(guò)時(shí)限制,涉及php字符串及數(shù)據(jù)庫(kù)相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
PHP 文件編程綜合案例-文件上傳的實(shí)現(xiàn)
本篇文章是對(duì)PHP中文件上傳的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07
php調(diào)用云片網(wǎng)接口發(fā)送短信的實(shí)現(xiàn)方法
這篇文章主要介紹了php調(diào)用云片網(wǎng)接口發(fā)送短信的實(shí)現(xiàn)方法的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10
PHP實(shí)現(xiàn)將HTML5中Canvas圖像保存到服務(wù)器的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)將HTML5中Canvas圖像保存到服務(wù)器的方法,可實(shí)現(xiàn)將Canvas圖像保存到服務(wù)器的功能,是非常實(shí)用的技巧,需要的朋友可以參考下2014-11-11
PHP編程中的Session阻塞問(wèn)題與解決方法分析
這篇文章主要介紹了PHP編程中的Session阻塞問(wèn)題與解決方法,簡(jiǎn)單分析了php session阻塞的原理、原因與簡(jiǎn)單解決方法,需要的朋友可以參考下2017-08-08
PHP基于二分法實(shí)現(xiàn)數(shù)組查找功能示例【循環(huán)與遞歸算法】
這篇文章主要介紹了PHP基于二分法實(shí)現(xiàn)數(shù)組查找功能,結(jié)合實(shí)例形式分析了while循環(huán)與遞歸調(diào)用算法實(shí)現(xiàn)二分查找功能的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-12-12
PHP中實(shí)現(xiàn)中文字符進(jìn)制轉(zhuǎn)換原理分析
中文字符編碼研究系列第四期,PHP實(shí)現(xiàn)中文字符進(jìn)制轉(zhuǎn)換原理分析,主要討論中文漢字轉(zhuǎn)換為十進(jìn)制和十六進(jìn)制的方法,并掌握轉(zhuǎn)換原理應(yīng)用于實(shí)際開(kāi)發(fā)。本文以GBK編碼字符為例,討論GBK編碼的字符轉(zhuǎn)換原理2011-12-12

