


A simple way to create and verify hashes in PHP, php_PHP tutorial
PHP中创建和验证哈希的简单方法实探,php
PHP 5.5.0 带来了一份完整的全新特性与函数的列表。全新API之一就是Password Hashing API.它包含4个函数:password_get_info(), password_hash(), password_needs_rehash(),和password_verify().让我们分步来了解每个函数。
我们首先讨论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
通过以上知识,你可以在新的 PHP 5.5.0 版本中迅速且安全的创建 hash 密码了。

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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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.

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

In this chapter, we are going to learn the following topics related to routing ?

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

Validator can be created by adding the following two lines in the controller.

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
