[英]PHP password protection secure enough
在我開始之前,我想再次提出這個問題而道歉,正如許多用戶所做的那樣,但通過我做過的研究,我對我發現的內容並不滿意。 我只希望能在這里找到一些非常有用的東西。
因為md5或sha1被認為是不好的做法(即使使用salt ???),我也試圖創建這個函數來哈希我的密碼
$password = $_POST['password']; // lets say that my password is: my_sercretp@ssword123
function encrypt_the_password($password){
$salt = "lorem_ipsumd0l0rs1t@m3tc0ns3ct3tur@d1p1sc1ng3lit";
return hash('sha256', $salt.$password);// can use also different algorithm like sha512 or whirlpool
}
$hashed_password = encrypt_the_password($password);
請注意,這個我在一個只有一個用戶的個人網站上使用它,我。 如果有多個用戶,我會想出這樣的事情:
$password = $_POST['password'];
function generate_salt() {
$salt = uniqid(md5("lorem_ipsumd0l0rs1t@m3tc0ns3ct3tur@d1p1sc1ng3lit".microtime()));
$salt = hash('sha256', $salt);// can use also different algorithm like sha512 or whirlpool
return $salt;
}
function encrypt_the_password($password,$salt){
return hash('sha256', $salt.$password);// can use also different algorithm like sha512 or whirlpool
}
$hashed_password = encrypt_the_password($password,generate_salt());
這是否足夠安全(在每種情況下)或者這可以改善更多???
我的編輯:我嘗試使用crypt()函數提出一些新的東西。 這是我的代碼,如果網站只有一個用戶,admin:
$password = $_POST['password'];
$salt = "L0r3mIpsUmD0l0rS1tAm3t";
$hashed_password = crypt($password', '$2a$12$' . $salt);
如果網站有多個用戶:
$password = $_POST['password'];
function generate_salt() {
$salt = uniqid(sha1("L0r3mIpsUmD0l0rS1tAm3tc0ns3CT3tur4d1p1sc1ng3lit".microtime()));
$salt = substr(sha1($salt), 0, 22);
return $salt;
}
$hashed_password = crypt($password', '$2a$12$' . generate_salt());
這樣可以還是需要改進???
通過不編寫自己的算法來改進它。 你的算法是不安全的,因為你的salt是常量的,你只需要使用SHA256的一次迭代進行散列,這在計算上很便宜。
相反,使用Bcrypt ,這在計算上既昂貴又由知道他們正在做什么的人驗證,所以它比你的解決方案更安全。
您應該使用PHP 5.5中內置的密碼函數。 ircmaxell有一個回退庫,它可以提供早期版本的PHP中的函數: https : //github.com/ircmaxell/password_compat
它將始終使用最新的哈希技術,以防萬一甚至為您更新記錄。 請務必閱讀此庫附帶的README。
不要自己進行散列函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.