Hướng dẫn what is password hash in php? - băm mật khẩu trong php là gì?

Mật khẩu băm là một kỹ thuật chuyển đổi một mật khẩu thành một chuỗi khác có tên là mật khẩu băm. Mật khẩu băm thường là một chiều, tức là chúng ta có thể đi đến mật khẩu gốc từ mật khẩu băm. Vì vậy, điều này là lý do tại sao chúng ta cần sử dụng băm để làm tất cả những thứ này, tại sao lại đi thêm một dặm nếu chúng ta có thể lưu mật khẩu của mình vào cơ sở dữ liệu dưới dạng một chuỗi đơn giản. Lý do duy nhất và duy nhất để làm tất cả những điều này là để tăng cường bảo mật vì tin tặc không ăn cắp thông tin xác thực từ trang web có giá trị của chúng tôi. Vì vậy, tại sao chúng tôi sử dụng các phương thức băm khác nhau để băm mật khẩu để bảo mật mật khẩu của chúng tôi trong khi tạo các trang web và lưu trữ cơ sở dữ liệu của chúng tôi. Trong PHP, có nhiều thuật toán mật mã khác nhau thường được sử dụng như MD5, Crypt, SHA1 và BCrypt. Và ngày nay được sử dụng phổ biến nhất là phương pháp bcrypt băm. Trong bài viết này, chúng tôi sẽ tìm hiểu về phương pháp bcrypt băm trong PHP.

PHP cung cấp chức năng băm mật khẩu chung để tạo hàm băm mật khẩu mới từ mật khẩu.

Syntax:

string password_hash(string $password, string $algo, array $options = [])

Ở đây, hàm password_hash chủ yếu lấy ba tham số là:password_hash function takes mainly three parameters that are:

  • $ Mật khẩu: Mật khẩu mà bạn muốn băm, nó cần một giá trị chuỗi.The password that you want to hash it takes a string value.
  • $ algo: Thuật toán mà bạn muốn sử dụng để băm mật khẩu. Sau đây là các thuật toán mật khẩu có sẵn trong PHP. The algorithm that you want to use to hash the password. Following are the password algorithms available in PHP.
    • Password_BCrypt: Nó sử dụng thuật toán Crypt_blowfish để tạo băm.It uses the CRYPT_BLOWFISH algorithm to create the hash.
    • Password_argon2i: Nó sử dụng thuật toán argon2i để băm.It uses the Argon2i algorithm for hashing.
    • Password_argon2ID: Nó sử dụng thuật toán argon2id để băm. It uses the Argon2id algorithm for hashing.
    • Password_default: Nó sử dụng thuật toán bcrypt để băm. It uses the bcrypt algorithm for hashing.
  • Tùy chọn $: Nó có giá trị muối Giá trị mặc định là giá trị muối ngẫu nhiên. Một giá trị muối, một chuỗi bổ sung mà chúng tôi nối vào một chuỗi trong khi băm.It takes a salt value The default value is a random salt value. A salt value, an extra string that we append to a string while hashing.

Giá trị trả về: Nó trả về chuỗi mật khẩu băm. It returns a hashed password string.

Example:

PHP

$password = "GeeksforGeeks";

echo "Password is:"

bool password_verify(string $password, string $hash)
0$password

echo

bool password_verify(string $password, string $hash)
4
bool password_verify(string $password, string $hash)
5

bool password_verify(string $password, string $hash)
6
bool password_verify(string $password, string $hash)
7$password
bool password_verify(string $password, string $hash)
9

echo

Original Password is: GeeksforGeeks
Hashed Password is: $2y$10$MQU3vDgoN10.JxyJ1m9UQOEqFy.Jg3D8tmHdZUAAkcpGFRwkbbLfi
Password is valid!
1;

echo

Original Password is: GeeksforGeeks
Hashed Password is: $2y$10$MQU3vDgoN10.JxyJ1m9UQOEqFy.Jg3D8tmHdZUAAkcpGFRwkbbLfi
Password is valid!
4
bool password_verify(string $password, string $hash)
5

bool password_verify(string $password, string $hash)
6
bool password_verify(string $password, string $hash)
7$password
Original Password is: GeeksforGeeks
Hashed Password is: $2y$10$MQU3vDgoN10.JxyJ1m9UQOEqFy.Jg3D8tmHdZUAAkcpGFRwkbbLfi
Password is valid!
9

echo

Original Password is: GeeksforGeeks
Hashed Password is: $2y$10$MQU3vDgoN10.JxyJ1m9UQOEqFy.Jg3D8tmHdZUAAkcpGFRwkbbLfi
Password is valid!
1;

echo

Original Password is: GeeksforGeeks
Hashed Password is: $2y$10$MQU3vDgoN10.JxyJ1m9UQOEqFy.Jg3D8tmHdZUAAkcpGFRwkbbLfi
Password is valid!
4
bool password_verify(string $password, string $hash)
5

bool password_verify(string $password, string $hash)
6
bool password_verify(string $password, string $hash)
7$password9

$password0

Output:

echo 4

bool password_verify(string $password, string $hash)
5GeeksforGeeks

Mật khẩu là: Geekforgeek $2y$10$V4Cvy4caGAQBLWbw8i/PHOK9lopTYoxyQze3aZ3ocw9dDvju7Wxoi

Mật khẩu băm sử dụng crypt_blowfish: $ 2Y $ 10 $ v4CVY4CAGAQBLWBW8I/PHOK9LOPTYOXYQZE3AZ3OCW9DDVJU7WXOI$argon2i$v=19$m=65536,t=4,p=1$Y2F2TVouVWplYVYucy9DSw$p164c28N85L5v1i8GISN1oao10ZzNm9e/JAyicRaX/w

Mật khẩu băm sử dụng argon2i: $ argon2i $ v = 19 $ m = 65536, t = 4, p = 1 $ y2f2tvouvwplyvyucy9d $2y$10$MQU3vDgoN10.JxyJ1m9UQOEqFy.Jg3D8tmHdZUAAkcpGFRwkbbLfi

Mật khẩu băm sử dụng bcrypt: $ 2y $ 10 $ mqu3vdgon10.jxyj1m9uqoeqfy.jg3d8tmhdzuaakcpgfrwkbblfiWe have not used PASSWORD_ARGON2ID because it is not available in standard PHP installation.

Lưu ý: Chúng tôi chưa sử dụng password_argon2ID vì nó không có sẵn trong cài đặt PHP tiêu chuẩn.PHP provides an inbuilt function called password_verify to match the hashed password to the original passwords.

Syntax:

bool password_verify(string $password, string $hash)

Parameters:

  • Để xác minh mật khẩu băm: PHP cung cấp chức năng sẵn có gọi là password_verify để khớp với mật khẩu băm với mật khẩu ban đầu.The password that we have hashed using a hashing algorithm.
  • $ Mật khẩu: Mật khẩu mà chúng tôi đã băm bằng thuật toán băm. The hashed password that we are going to verify with the original password.

Example:

PHP

$password = "GeeksforGeeks";

echo "Password is:"

bool password_verify(string $password, string $hash)
0$password

$password8;

echo

bool password_verify(string $password, string $hash)
4
bool password_verify(string $password, string $hash)
5

echo

Original Password is: GeeksforGeeks
Hashed Password is: $2y$10$MQU3vDgoN10.JxyJ1m9UQOEqFy.Jg3D8tmHdZUAAkcpGFRwkbbLfi
Password is valid!
1;

echo

Original Password is: GeeksforGeeks
Hashed Password is: $2y$10$MQU3vDgoN10.JxyJ1m9UQOEqFy.Jg3D8tmHdZUAAkcpGFRwkbbLfi
Password is valid!
4
bool password_verify(string $password, string $hash)
5

echo

Original Password is: GeeksforGeeks
Hashed Password is: $2y$10$MQU3vDgoN10.JxyJ1m9UQOEqFy.Jg3D8tmHdZUAAkcpGFRwkbbLfi
Password is valid!
1;

echo

Original Password is: GeeksforGeeks
Hashed Password is: $2y$10$MQU3vDgoN10.JxyJ1m9UQOEqFy.Jg3D8tmHdZUAAkcpGFRwkbbLfi
Password is valid!
4
bool password_verify(string $password, string $hash)
5

echo 4

bool password_verify(string $password, string $hash)
5

Mật khẩu là: Geekforgeek

Mật khẩu băm sử dụng crypt_blowfish: $ 2Y $ 10 $ v4CVY4CAGAQBLWBW8I/PHOK9LOPTYOXYQZE3AZ3OCW9DDVJU7WXOI

echo3

$password0

Mật khẩu băm sử dụng argon2i: $ argon2i $ v = 19 $ m = 65536, t = 4, p = 1 $ y2f2tvouvwplyvyucy9d

Original Password is: GeeksforGeeks
Hashed Password is: $2y$10$MQU3vDgoN10.JxyJ1m9UQOEqFy.Jg3D8tmHdZUAAkcpGFRwkbbLfi
Password is valid!


Mật khẩu băm là gì?

Mật khẩu băm được định nghĩa là đặt mật khẩu thông qua thuật toán băm (BCRYPT, SHA, v.v.) để biến bản rõ thành một chuỗi số và chữ cái không thể hiểu được. Điều này rất quan trọng đối với vệ sinh bảo mật cơ bản bởi vì, trong trường hợp vi phạm bảo mật, bất kỳ mật khẩu bị xâm phạm nào đều không thể hiểu được đối với diễn viên xấu.putting a password through a hashing algorithm (bcrypt, SHA, etc) to turn plaintext into an unintelligible series of numbers and letters. This is important for basic security hygiene because, in the event of a security breach, any compromised passwords are unintelligible to the bad actor.

Việc sử dụng băm trong PHP là gì?

Định nghĩa và sử dụng hàm Hash () trả về giá trị băm cho dữ liệu đã cho dựa trên thuật toán như (MD5, SHA256). Giá trị trả về là một chuỗi có hình lục giác (giá trị thập lục phân).returns a hash value for the given data based on the algorithm like (md5, sha256). The return value is a string with hexits (hexadecimal values).

Mật khẩu PHP có an toàn không?

Kết quả băm từ password_hash () là an toàn vì: Nó sử dụng thuật toán băm mạnh mẽ.Nó thêm một muối ngẫu nhiên để ngăn chặn các bảng cầu vồng và các cuộc tấn công từ điển. because: It uses a strong hashing algorithm. It adds a random salt to prevent rainbow tables and dictionary attacks.

Bạn có thể giải mã mật khẩu băm PHP không?

Giải mã mật khẩu: Để giải mã mật khẩu băm và truy xuất chuỗi gốc, chúng tôi sử dụng hàm password_verify ().Hàm password_verify () xác minh rằng băm đã cho khớp với mật khẩu đã cho, được tạo bởi hàm password_hash ().To decrypt a password hash and retrieve the original string, we use the password_verify() function. The password_verify() function verifies that the given hash matches the given password, generated by the password_hash() function.