Hướng dẫn bcrypt password php - mật khẩu bcrypt php

Cập nhật ngày 27/12/2021

Trong PHP, muốn sử dụng mã hoá Bcrypt ta sẽ dùng hàm password_hash(). Hàm này thường dùng để mã hoá mật khẩu.password_hash(). Hàm này thường dùng để mã hoá mật khẩu.

Ngoài mã hoá Bcrypt hàm này còn hỗ trợ mã hoá Argon2i và Argon2id.

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

Trong đó:

  • $password: chuỗi cần mã hoá: chuỗi cần mã hoá
  • $algo: Phương thức mã hoá (mặc định là Bcrypt). (xem thêm) Phương thức mã hoá (mặc định là Bcrypt). (xem thêm)
  • $options: mảng tùy chọn (xem thêm) mảng tùy chọn (xem thêm)

Kết quả:

Trả về chuỗi mã hoá hoặc FALSE nếu thất bại.FALSE nếu thất bại.

Ví dụ:

 12,
];

echo password_hash("rasmuslerdorf", PASSWORD_BCRYPT, $options);

So sánh 2 chuỗi đã mã hoá?

Đồi khi ta buồn ta chả biết làm gì rồi ngồi vu vơ nghĩ về Bcrypt, khi mà cùng 1 chuỗi nó mã hoá ra nhiều chuỗi mới khác nhau thì làm sao so sánh?

Nhưng không sao, PHP đã cung cấp cho ta 1 hàm giúp làm việc này đó là password_verify().

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

Trong đó:

  • $password: chuỗi cần mã hoá: chuỗi gốc cần so sánh
  • $algo: Phương thức mã hoá (mặc định là Bcrypt). (xem thêm): chuỗi đã má hoá

Kết quả:

Trả về chuỗi mã hoá hoặc FALSE nếu thất bại.

Ví dụ:

Cập NHật ngày 27/12/2021

Nội phân chính

  • Vậy SánH 2 Chuỗi Đạo MÃ Hoá?
  • Vậy SánH 2 Chuỗi Đạo MÃ Hoá?
  • Đối mặt với nó, mật mã là khó.
  • API Php 5.5 - (có sẵn cho 5.3.7+)
  • Zend \ Crypt \ Mật khẩu \ BCRYPT (5.3.2+)
  • Mật khẩu
  • Về bcrypt

Nếu bạn nhận thấy, mỗi một trong số các thư viện này sẽ trả về một chuỗi. Đó là vì cách bcrypt hoạt động nội bộ. Và có rất nhiều câu trả lời về điều đó. Dưới đây là một lựa chọn mà tôi đã viết, rằng tôi sẽ không sao chép/dán ở đây, nhưng liên kết đến:password_hash(). Hàm này thường dùng để mã hoá mật khẩu.

Sự khác biệt cơ bản giữa các thuật toán băm và mã hóa - giải thích thuật ngữ và một số thông tin cơ bản về chúng.

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

Về việc đảo ngược các băm không có bàn cầu vồng - về cơ bản tại sao chúng ta nên sử dụng bcrypt ngay từ đầu ...

  • Lưu trữ băm BCRYPT - Về cơ bản tại sao muối và thuật toán được bao gồm trong kết quả băm.: chuỗi cần mã hoá
  • Cách cập nhật chi phí của BCRYPT băm - về cơ bản là cách chọn và sau đó duy trì chi phí của bcrypt băm. Phương thức mã hoá (mặc định là Bcrypt). (xem thêm)
  • Cách băm mật khẩu dài với BCRYPT - Giải thích giới hạn mật khẩu 72 ký tự của BCRYPT. mảng tùy chọn (xem thêm)

Cách bcrypt sử dụng muối

Thực tiễn tốt nhất về mật khẩu muối và tiêu - về cơ bản, không sử dụng "hạt tiêu"FALSE nếu thất bại.

Di chuyển mật khẩu

 12,
];

echo password_hash("rasmuslerdorf", PASSWORD_BCRYPT, $options);
6 cũ sang bcrypt

 12,
];

echo password_hash("rasmuslerdorf", PASSWORD_BCRYPT, $options);

Vậy SánH 2 Chuỗi Đạo MÃ Hoá?

Trong PHP, Muốn Sử dụng MÃ Hoá Bcrypt ta Sẽ Dùng Hài mật khẩu_hash (). HÀM NÀY thường Dùng Để MÃ HOÁ MậT KHẩU.

NGOào

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

Về việc đảo ngược các băm không có bàn cầu vồng - về cơ bản tại sao chúng ta nên sử dụng bcrypt ngay từ đầu ...

  • Lưu trữ băm BCRYPT - Về cơ bản tại sao muối và thuật toán được bao gồm trong kết quả băm.: chuỗi gốc cần so sánh
  • Cách cập nhật chi phí của BCRYPT băm - về cơ bản là cách chọn và sau đó duy trì chi phí của bcrypt băm.: chuỗi đã má hoá

Cách bcrypt sử dụng muối

Thực tiễn tốt nhất về mật khẩu muối và tiêu - về cơ bản, không sử dụng "hạt tiêu"

Di chuyển mật khẩu

 12,
];

echo password_hash("rasmuslerdorf", PASSWORD_BCRYPT, $options);
6 cũ sang bcrypt

Cập NHật ngày 27/12/2021

Nội phân chính

  • Vậy SánH 2 Chuỗi Đạo MÃ Hoá?
  • Đối mặt với nó, mật mã là khó.
  • API Php 5.5 - (có sẵn cho 5.3.7+)
  • Zend \ Crypt \ Mật khẩu \ BCRYPT (5.3.2+)
  • Mật khẩu
  • Về bcrypt

Nếu bạn nhận thấy, mỗi một trong số các thư viện này sẽ trả về một chuỗi. Đó là vì cách bcrypt hoạt động nội bộ. Và có rất nhiều câu trả lời về điều đó. Dưới đây là một lựa chọn mà tôi đã viết, rằng tôi sẽ không sao chép/dán ở đây, nhưng liên kết đến:password_hash(). Hàm này thường dùng để mã hoá mật khẩu.

Ngoài mã hoá Bcrypt hàm này còn hỗ trợ mã hoá Argon2i và Argon2id.

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

Trong đó:

  • $password: chuỗi cần mã hoá: chuỗi cần mã hoá
  • $algo: Phương thức mã hoá (mặc định là Bcrypt). (xem thêm) Phương thức mã hoá (mặc định là Bcrypt). (xem thêm)
  • $options: mảng tùy chọn (xem thêm) mảng tùy chọn (xem thêm)

Kết quả:

Trả về chuỗi mã hoá hoặc FALSE nếu thất bại.FALSE nếu thất bại.

Ví dụ:

 12,
];

echo password_hash("rasmuslerdorf", PASSWORD_BCRYPT, $options);

So sánh 2 chuỗi đã mã hoá?

Đồi khi ta buồn ta chả biết làm gì rồi ngồi vu vơ nghĩ về Bcrypt, khi mà cùng 1 chuỗi nó mã hoá ra nhiều chuỗi mới khác nhau thì làm sao so sánh?

Nhưng không sao, PHP đã cung cấp cho ta 1 hàm giúp làm việc này đó là password_verify().

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

Trong đó:

  • $password: chuỗi cần mã hoá: chuỗi gốc cần so sánh
  • $algo: Phương thức mã hoá (mặc định là Bcrypt). (xem thêm): chuỗi đã má hoá

Kết quả:

Trả về chuỗi mã hoá hoặc FALSE nếu thất bại.

Ví dụ:

So sánh 2 chuỗi đã mã hoá?Awesome! However, like other areas of cryptography, you shouldn't be doing it yourself. If you need to worry about anything like managing keys, or storing salts or generating random numbers, you're doing it wrong.

Đồi khi ta buồn ta chả biết làm gì rồi ngồi vu vơ nghĩ về Bcrypt, khi mà cùng 1 chuỗi nó mã hoá ra nhiều chuỗi mới khác nhau thì làm sao so sánh?

Nhưng không sao, PHP đã cung cấp cho ta 1 hàm giúp làm việc này đó là password_verify().

$password: chuỗi gốc cần so sánh

$hash: chuỗi đã má hoá

TRUE nếu khớp, FALSE nếu không khớp.

So, you want to use bcrypt? Awesome! However, like other areas of cryptography, you shouldn't be doing it yourself. If you need to worry about anything like managing keys, or storing salts or generating random numbers, you're doing it wrong.

The reason is simple: it's so trivially easy to screw up bcrypt. In fact, if you look at almost every piece of code on this page, you'll notice that it's violating at least one of these common problems.

Face It, Cryptography is hard.

 12,
];

echo password_hash("rasmuslerdorf", PASSWORD_BCRYPT, $options);
2

Leave it for the experts. Leave it for people whose job it is to maintain these libraries. If you need to make a decision, you're doing it wrong.

Resources:

  • Instead, just use a library. Several exist depending on your requirements.
  • Libraries
  • Here is a breakdown of some of the more common APIs.

PHP 5.5 API - (Available for 5.3.7+)

Starting in PHP 5.5, a new API for hashing passwords is being introduced. There is also a shim compatibility library maintained (by me) for 5.3.7+. This has the benefit of being a peer-reviewed and simple to use implementation.

 12,
];

echo password_hash("rasmuslerdorf", PASSWORD_BCRYPT, $options);
3

Resources:

  • Really, it's aimed to be extremely simple.
  • Documentation: on PHP.net

Compatibility Library: on GitHub

PHP's RFC: on wiki.php.net

 12,
];

echo password_hash("rasmuslerdorf", PASSWORD_BCRYPT, $options);
4

References:

  • Zend\Crypt\Password\Bcrypt (5.3.2+)

This is another API that's similar to the PHP 5.5 one, and does a similar purpose.

Documentation: on Zend

 12,
];

echo password_hash("rasmuslerdorf", PASSWORD_BCRYPT, $options);
5

Blog Post: Password Hashing With Zend Crypt

  • PasswordLib
  • This is a slightly different approach to password hashing. Rather than simply supporting bcrypt, PasswordLib supports a large number of hashing algorithms. It's mainly useful in contexts where you need to support compatibility with legacy and disparate systems that may be outside of your control. It supports a large number of hashing algorithms. And is supported 5.3.2+
  • Source Code / Documentation: GitHub

PHPASS Don't use the PHPASS alternatives that are not hosted on openwall, they are different projects!!!

This is a layer that does support bcrypt, but also supports a fairly strong algorithm that's useful if you do not have access to PHP >= 5.3.2... It actually supports PHP 3.0+ (although not with bcrypt).

Resources

  • Code: cvsweb
  • Project Site: on OpenWall
  • A review of the < 5.3.0 algorithm: on StackOverflow
  • Note: Don't use the PHPASS alternatives that are not hosted on openwall, they are different projects!!!
  • About BCrypt
  • If you notice, every one of these libraries returns a single string. That's because of how BCrypt works internally. And there are a TON of answers about that. Here are a selection that I've written, that I won't copy/paste here, but link to:
  • Fundamental Difference Between Hashing And Encryption Algorithms - Explaining the terminology and some basic information about them.
  • About reversing hashes without rainbow tables - Basically why we should use bcrypt in the first place...

Storing bcrypt Hashes - basically why is the salt and algorithm included in the hash result.

How to update the cost of bcrypt hashes - basically how to choose and then maintain the cost of the bcrypt hash.HIGHLY recommend that you use one of the above libraries for handling this for you.

How to hash long passwords with bcrypt - explaining the 72 character password limit of bcrypt.

How bcrypt uses salts