PHP 5.5 创建和验证哈希最简单的方法详解_PHP
我们首先讨论password_hash()函数。这将用作创建一个新的密码的哈希值。它包含三个参数:密码、哈希算法、选项。前两项为必须的。你可以根据下面的例子来使用这个函数:
复制代码 代码如下:
$password = 'foo';
$hash = password_hash($password,PASSWORD_BCRYPT);
//$2y$10$uOegXJ09qznQsKvPfxr61uWjpJBxVDH2KGJQVnodzjnglhs2WTwHu
你将注意到我们并没有给这个哈希加任何选项。现在可用的选项被限定为两个: cost 和salt。妖添加选项你需要创建一个关联数组。
复制代码 代码如下:
$options = [ 'cost' => 10,
'salt' => mcrypt_create_iv(22, MCRYPT_DEV_URANDOM) ];
将选项添加到 password_hash() 函数后,我们的哈希值变了,这样更加安全。
复制代码 代码如下:
$hash = password_hash($password,PASSWORD_BCRYPT,$options);
//$2y$10$JDJ5JDEwJDhsTHV6SGVIQuprRHZnGQsUEtlk8Iem0okH6HPyCoo22
现在哈希创建完毕了,我们可以通过 password_get_info() 查看新建哈希值得相关信息。password_get_info() 需要一个参数——哈希值——并返回一个包含算法(所用哈希算法的整数代表形式)、算法名(所用哈希算法的可读名称)以及选项(我们用于创建哈希值得选项)的关联数组。
复制代码 代码如下:
var_dump(password_get_info($hash));
/*
array(3) {
["algo"]=>
int(1)
["algoName"]=>
string(6) "bcrypt"
["options"]=>
array(1) {
["cost"]=>
int(10)
}
}
*/
先一个被添加到 Password Hashing API 的是 password_needs_rehash(),它接受三个参数,hash、hash 算法以及选项,前两个是必填项。 password_needs_rehash()用来检查一个hash值是否是使用特定算法及选项创建的。这在你的数据库受损需要调整hash时非常有用。通过利用 password_needs_rehash() 检查每个hash值,我们可以看到已存的hash 值是否匹配新的参数, 仅影响那些使用旧参数创建的值。
最后,我们已经创建了我们的hash值,查阅了它如何被创建,查阅了它是否需要被重新hash,现在我们需要验证它。要验证纯文本到其hash值,我们必须使用 password_verify(),它需要两个参数,密码及hash值,并将返回 TRUE 或 FALSE。让我们检查一次我们获得的 hashed 看看是否正确。
复制代码 代码如下:
$authenticate = password_verify('foo','$2y$10$JDJ5JDEwJDhsTHV6SGVIQuprRHZnGQsUEtlk8Iem0okH6HPyCoo22');
//TRUE
$authenticate = password_verify('bar','$2y$10$JDJ5JDEwJDhsTHV6SGVIQuprRHZnGQsUEtlk8Iem0okH6HPyCoo22');
//FALSE

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,
