Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create and validate hashes with PHP 5.5

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to create and verify hashes with PHP 5.5". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to create and verify hashes with PHP 5.5".

Let's start with the password_hash () function. This will be used as a hash value to create a new password. It contains three parameters: password, hash algorithm, and option. The first two items are necessary. You can use this function according to the following example:

The copy code is as follows:

$password = 'foo'

$hash = password_hash ($password,PASSWORD_BCRYPT)

/ / $2y$10 $uOegXJ09qznQsKvPfxr61uWjpJBxVDH2KGJQVnodzjnglhs2WTwHu

You will notice that we did not add any options to this hash. The options available are now limited to two: cost and salt. Demon add option you need to create an associative array.

The copy code is as follows:

$options = ['cost' = > 10

'salt' = > mcrypt_create_iv (22, MCRYPT_DEV_URANDOM)]

After adding the option to the password_hash () function, our hash value has changed, which is more secure.

The copy code is as follows:

$hash = password_hash ($password,PASSWORD_BCRYPT,$options)

/ / $2y$10 $JDJ5JDEwJDhsTHV6SGVIQuprRHZnGQsUEtlk8Iem0okH6HPyCoo22

Now that the hash has been created, we can view the information about the new hash value through password_get_info (). Password_get_info () takes a parameter-- a hash-- and returns an associative array containing the algorithm (the integer representation of the hash algorithm used), the algorithm name (the readable name of the hash algorithm used), and the options (we used to create the hash worth option).

The copy code is as follows:

Var_dump (password_get_info ($hash))

/ *

Array (3) {

["algo"] = >

Int (1)

["algoName"] = >

String (6) "bcrypt"

["options"] = >

Array (1) {

["cost"] = >

Int (10)

}

}

, /

The first one to be added to Password Hashing API is password_needs_rehash (), which accepts three parameters, hash, hash algorithm, and options, the first two of which are required. Password_needs_rehash () is used to check whether a hash value is created using a specific algorithm and options. This is very useful when your database is corrupted and you need to adjust hash. By checking each hash value with password_needs_rehash (), we can see whether the existing hash values match the new parameters, affecting only those values created with the old parameters.

Finally, we have created our hash value, looked at how it was created, looked at whether it needs to be re-hash, and now we need to validate it. To validate plain text to its hash value, we must use password_verify (), which requires two parameters, a password and a hash value, and will return TRUE or FALSE. Let's check the hashed we got to see if it is correct.

The copy code is as follows:

$authenticate = password_verify ('foo','$2y$10 $JDJ5JDEwJDhsTHV6SGVIQuprRHZnGQsUEtlk8Iem0okH6HPyCoo22')

/ / TRUE

$authenticate = password_verify ('bar','$2y$10 $JDJ5JDEwJDhsTHV6SGVIQuprRHZnGQsUEtlk8Iem0okH6HPyCoo22')

/ / FALSE

Thank you for your reading, the above is the content of "how to create and verify hashes with PHP 5.5". After the study of this article, I believe you have a deeper understanding of how to create and verify hashes with PHP 5.5. specific usage still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report