1 votes

Given the Python hash (), is there similar in PHP?

I have a python script, which performs a login and pass, which is stored with hash in Mysql, with the following code:

 pass = hash(self.get_argument("psw", 'dato'))

generating a hash type -5995266028892256335

Now I need to login from PHP, I understand that the hash function exists in php:

 echo hash('md5', 'mipass');

but I can't determine in what format Python is generating it.


Patricio Moracho Points 24098

The problem is that Python hash() is NOT a routine cryptographic, it is simply an internal routine to return an integer value that works as a unique identifier of the object. A same password in different executions or instances of the Pyhton will give multiple hash. for example:

c:> python -c "print(hash('hola'))"
c:> 161768099
c:> python -c "print(hash('hola'))"
c:> -339335518

What you can do is to use the module hashlib the following form:

import hashlib
hash = hashlib.sha256("contraseña").hexdigest()

> edf9cf90718610ee7de53c0dcc250739239044de9ba115bb0ca6026c3e4958a5

The received string will be your hash in order to save in the database. In PHP if there is the routine hash() as cryptographic, the way you repeat the code above would look like this:

echo hash('sha256', 'contraseña');



The choice of hash algorithm is a topic, md5 is extremely fast and easy to calculate, so that brute force attacks are totally feasible, I suggest that notes sha256 up, generate hashes longer and more difficult to solve by brute force. Any way these algorithm are general-purpose and were not designed specifically to solve passwords, if we add that the computational power continues to grow, a hash considered safe today may not be tomorrow. Today is usually recommend other algorithms to treat the passwords, I recommend this document.


Marcelo Points 26

I do not know this function hash () to create an md5 hash would be as follows:

import hashlib
hash = hashlib.md5("contraseña")


Edit: however to avoid changing a lot you could also create or modify the hash function and to use hashlib within, for example:

def hash(texto):
    return hashlib.md5(texto)


It is true, how to say Patrick, I thought of hash as a user-defined function, it is not a good idea, and in the code I miss hexdigest:

import hashlib
hash = hashlib.md5("contraseña").hexdigest()

Thank you very much.


HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by: