Check if the password hash generated by PHP matches in Python

environment

PHP 7.1 Python 2.7.6

What you want to check

I'm currently developing in PHP and using password_hash to encrypt passwords. A long time ago, I wondered if encrypted passwords could be used in the same way when migrating to another language (Python in this case) in the future: question: In other words, I want to make sure that the user's password can be verified normally and I can log in even after migrating to Python. To put it a little more microscopically, password_hash uses an encryption algorithm called Blowfish (see below), so I'd like to see if Blowfish can also be used in Python for matching.

Encrypt with PHP

.php


password_hash('password', PASSWORD_DEFAULT);
// $2y$10$BN2hH0B3gnZceNlW1JXiNOUN8NWybLlfqZh6WQ/imah4htM8fktFW

password_hash('password', PASSWORD_BCRYPT);
// $2y$10$CuZkO0N29B1YtHHI9mwvIOCSUitQh4ptyfxYWvHhHoHHP2GZqC5Ga

Commentary

password_hash currently allows you to specify two types of constants: PASSWORD_DEFAULT and PASSWORD_BCRYPT. http://php.net/manual/ja/function.password-hash.php

I checked the contents of the constant (though it's a stub)

/php/lib/php.jar!/stubs/standard/password.php


define("PASSWORD_DEFAULT", 1);
define("PASSWORD_BCRYPT", 1);

** After all, both PASSWORD_DEFAULT and PASSWORD_BCRYPT point to the same value. ** ** In this case, it will be implemented in Bcrypt. Since Bcrypt ≒ Blowfish, the current encryption algorithm for password_hash will always be Blowfish.

Check if it can be matched with Python

import bcrypt

password = b'password'
phpHash = '$2y$10$BN2hH0B3gnZceNlW1JXiNOUN8NWybLlfqZh6WQ/imah4htM8fktFW'

if bcrypt.checkpw(password, phpHash):
    print("It Matches!")
else:
    print("It Does not Match :(")

# It Matches!

** Matched successfully: ok_woman_tone1: **

Commentary

--Use the bcrypt module to check if the PHP-encrypted password matches --You need to install a module called bcrypt in advance. - https://pypi.python.org/pypi/bcrypt/3.1.3 -- bcrypt.checkpw corresponds to password_verify in PHP

Bean knowledge (I just didn't know)

The encrypted version (leftmost part) of the value generated using PHP's password_hash is$ 2y $. However, when generated using Python's bcrypt module, it seems that only $ 2a $ and $ 2b $ can be specified as the encrypted version.

salt = bcrypt.gensalt(rounds=10, prefix=b'2a')
password = b'password'
hashed = bcrypt.hashpw(password, salt)

Isn't this the same password? I thought: rolling_eyes: but it matched as mentioned above. On the contrary, even if the value generated by bcrypt.hashpw is matched with password_verify, it matches successfully. In other words, even if the encrypted versions are different, such as $ 2a $ and $ 2y $, they will be recognized as the same password, so when you move to Python in the future, you will bother to replace $ 2y $ with $ 2y $. No processing such as replacing with $ 2a $ `is required.

Recommended Posts

Check if the password hash generated by PHP matches in Python
Check if the URL exists in Python
How to check if the contents of the dictionary are the same in Python by hash value
Check if the characters are similar in Python
Check if the string is a number in python
[python] How to check if the Key exists in the dictionary
Check the behavior of destructor in Python
Read the file line by line in Python
Read the file line by line in Python
Get the last element of the array by splitting the string in Python and PHP
Check if the expected column exists in Pandas DataFrame
Check if it is Unix in the scripting language
Shift the alphabet string by N characters in Python
How to check in Python if one of the elements of a list is in another list
Store the stock price scraped by Python in the DB
Check if it is Unix in the scripting language
Check the asymptotic nature of the probability distribution in Python
[Python] Open the csv file in the folder specified by pandas
Check the operation of Python for .NET in each environment
Master the type in Python? (When should type check be done)
Visualize the correlation matrix by principal component analysis in Python
Download the file in Python
Easy password box in Python
[Python] Check the installed libraries
Sort by date in python
If I thought I didn't see the pyc file recently, it was quarantined in pycache by python3.
python Note: Determine if command line arguments are in the list
Check if the configuration file is read in an easy-to-understand manner
[Understanding in the figure] Management of Python virtual environment by Pipenv
How to check the memory size of a variable in Python
Delete a particular character in Python if it is the last
Read the standard output of a subprocess line by line in Python
How to check the memory size of a dictionary in Python
Sort the file names obtained by Python glob in numerical order
Check the drawing result using Plotly by embedding CodePen in Qiita
Check if you can connect to a TCP port in Python
Have python check if the string can be converted / converted to int
[Python] Do not put Japanese in the path used by OpenCV
[Golang] Check if a specific character string is included in the character string
Play by hitting the Riot Games API in Python First half
I want to receive the configuration file and check if the JSON file generated by jinja2 is a valid JSON
How to automatically check if the code you wrote in Google Colaboratory corresponds to the python coding standard "pep8"
Check for memory leaks in Python
[Python] Check the current directory, move the directory
Getting the arXiv API in Python
Check for external commands in python
Python in the browser: Brython's recommendation
Save the binary file in Python
Get the desktop path in Python
Get the script path in Python
In the python command python points to python3.8
Implement the Singleton pattern in Python
Check and move directories in Python
Hit the web API in Python
Hash method (open address method) in Python
Check the data summary in CASTable
I wrote the queue in Python
Calculate the previous month in Python
Examine the object's class in python
Get the desktop path in Python
Password generation in texto with python