Hướng dẫn dùng salted password trong PHP
Tìm hiểu khi sử dụng các hàm băm tạo dữ liệu lưu trữ passwordKhi 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ụ: Show $raw_password = 'abc123'; $crypt = md5($raw_password); //e99a18c428cb38d5f260853678922e03 Ví dụ trên, đã sử dụng hàm băm của PHP là 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 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à 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 Tìm hiểu khi sử dụng các hàm băm tạo dữ liệu lưu trữ passwordKhi 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à 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 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à 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 (PHP 5 >= 5.5.0, PHP 7, PHP 8) password_hash — Creates a password hash Descriptionpassword_hash(string The following algorithms are currently supported:
Supported options for
Supported options for
Parameterspassword The user's password. Caution Using the algo A password algorithm constant denoting the algorithm to use when hashing the password. options An associative array containing options. See the password algorithm constants for documentation on the supported options for each algorithm. If omitted, a random salt will be created and the default cost will be used. Return ValuesReturns the hashed password. The used algorithm, cost and salt are returned as part of the hash. Therefore, all information that's needed to verify the hash is included in it. This allows the password_verify() function to verify the hash without needing separate storage for the salt or algorithm information. Changelog
ExamplesExample #1 password_hash() example
The above example will output something similar to: $2y$10$.vGA1O9wmRjrwAVXD98HNOgsNpDczlqm3Jq7KnEd1rVAGv3Fykk1a Example #2 password_hash() example setting cost manually
The above example will output something similar to: $2y$12$QjSH496pcT5CEbzjD/vtVeH03tfHKFy36d4J0Ltp3lRtee9HDxY3K Example #3 password_hash() example finding a good cost
"Appropriate Cost Found: " . $cost; The above example will output something similar to: Appropriate Cost Found: 10 Example #4 password_hash() example using Argon2i
The above example will output something similar to: Argon2i hash: $argon2i$v=19$m=1024,t=2,p=2$YzJBSzV4TUhkMzc3d3laeg$zqU/1IN0/AogfP4cmSJI1vc8lpXRW9/S0sYY2i2jHT0 NotesCaution It is strongly recommended that you do not generate your own salt for this function. It will create a secure salt automatically for you if you do not specify one. As noted above, providing the
See Also
phpnetcomment201908 at lucb1e dot com ¶ 3 years ago
nicoSWD ¶ 8 years ago
Lyo Mi ¶ 6 years ago
Cloxy ¶ 8 years ago
= mcrypt_create_iv(22, MCRYPT_DEV_URANDOM); martinstoeckli ¶ 9 years ago
Mike Robinson ¶ 8 years ago
ms1 at rdrecs dot com ¶ 2 years ago
php dot net at marksim dot org ¶ 2 years ago
Anonymous ¶ 3 years ago
Anonymous ¶ 2 years ago
hman ¶ 2 years ago
|