Hướng dẫn best hash algorithm php

Tìm hiểu khi sử dụng các hàm băm tạo dữ liệu lưu trữ password

Khi lưu trữ password vào CSDL thường sẽ sử dụng các hàm băm khác nhau được hỗ trợ bởi hệ CSDL hoặc ngôn ngữ lập trình (như MD5, SHA1 ...) để tạo dữ liệu mã hóa, dữ liệu mã hóa đó được lưu vào CSDL. Ví dụ:

$raw_password = 'abc123'; $crypt = md5($raw_password); //e99a18c428cb38d5f260853678922e03

Ví dụ trên, đã sử dụng hàm băm của PHP là md5 để mã hóa password abc123, kết quả mã hóa là e99a18c428cb38d5f260853678922e03

Bởi vì hàm băm tạo ra các giá trị không thể dịch ngược (không có thuật toán để giải giá trị hash e99a18c428cb38d5f260853678922e03 là chuỗi abc123, chỉ duy nhất một cách là thử), nên có cảm giác sẽ an toàn. Tuy nhiên với các mật khẩu yếu, nó có thể bị dò ra dựa trên giá trị băm của các mật khẩu phổ biến biết trước. Như trường hợp trên khi thấy e99a18c428cb38d5f260853678922e03 thì đoán được password là abc123. Để khắc phục điều này có thể sử dụng đến salt

Sử dụng Salt tăng cường an toàn cho mật khẩu

Để phức tạp hóa mật khẩu lưu trữ, thì các mật khẩu gốc trước khi mã hóa được nối thêm các chuỗi, các chuỗi thêm này gọi là salt

Ví dụ:

$raw_password = 'abc123'; //Sinh ra chuỗi dài 32 ngẫu nhiên, cũng cần lưu chuỗi này vào một cột trong DB $salt = random_bytes(32); //Sử dụng thêm một salt cố định $staticSalt = 'G4334#'; $crypt = md5($staticSalt.$raw_password.$salt);

Giờ mật khẩu lưu trữ ở trên phức tạp hơn rất nhiều. Biết được $crypt đoán ra $raw_password là rất khó, kể cả khi là password yếu. Khó mà xây dựng được một từ điển chứa các mã hóa tương ứng với password.

Which is the best recommended algorithm to use for encrypting passwords in php/mysql

asked Jan 12, 2010 at 10:45

3

SHA-512 with a salt is a good & secure way to hash a password. If that's not available you have SHA-1 but it's security is considered a bit weak these days, especially if you don't use a salt.

answered Jan 12, 2010 at 12:47

Hướng dẫn best hash algorithm php

TravisOTravisO

9,2804 gold badges34 silver badges44 bronze badges

14

  1. Current thinking is to use a SLOW hash algo. This causes "brute forcers" to spend lots of time generating all those attempts.

  2. Much smarter still is to track URI requests by IP and block with explanation when 5 login attempts fail from same IP within any given 5 minute period.

  3. Bank-smarter still is to do #1, #2 and also require a secondary pass challenge once the first one succeeds. Triple failure at second challenge results in lock-out.

Level 3 security is very, very strong. Probably too strong.

answered Dec 7, 2011 at 21:09

FYAFYA

4024 silver badges6 bronze badges

1

There's a decent article here - short answer, use crypt(), and make sure you use a salt.

answered Jan 12, 2010 at 10:50

Dominic RodgerDominic Rodger

95.1k33 gold badges196 silver badges211 bronze badges

2

I would use the php's crypt() function because there will not be anyway for the password to be decrypted. When I need to check the newly entered password I just have to encrypt that one and compare the two results

answered Jan 12, 2010 at 10:49

phunehehephunehehe

8,4787 gold badges48 silver badges78 bronze badges

5

Miki725 raises interesting points with the Matasano article Whilst sha512 is better than md5 cryptographically, bcrypt beats them all because it is slower and thus costs more to attack. Slower is not bad the internet is slow already, it's millions of times slower than CPU cache, and thousands of times slower than disk. Making password checks take 200ms instead of 1ms to compute is not going to bother any users.

Most importantly do not forget to use a nonce that is randomly generated and different for each user.

bcrypt is going to be sub-optimal in PHP because php is interpreted and this gives the attacker some advantage but there's a how to in this stackoverflow article

answered Jun 13, 2012 at 22:34

user340140user340140

5885 silver badges9 bronze badges

There are a lot of options - see the php hash docs for the complete list.

Speed is not an advantage, so using sha-512 or whirlpool is a good idea. You don't have to store the full length of the hash in mysql, for instance you could hash something as whirlpool, which is 128 characters long, and store only the first 64 characters for efficiency.

answered Jan 12, 2010 at 12:53

Hướng dẫn best hash algorithm php

JALJAL

21k1 gold badge46 silver badges65 bronze badges

3

Not the answer you're looking for? Browse other questions tagged php mysql or ask your own question.