Given the hash () of Python, does it exist similar in PHP?

1

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 there is a hash function in php:

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

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

    
asked by Alejandro 26.01.2018 в 13:54
source

2 answers

1

The problem is that in Python hash() it is NOT a cryptographic routine, it is simply an internal routine to return an integer value that functions as the unique identifier of the object. The same password in different executions or instances of Pyhton will give you 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 use the module hashlib in the following way:

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

> edf9cf90718610ee7de53c0dcc250739239044de9ba115bb0ca6026c3e4958a5

The received string will be your hash to save in the database. In PHP if the routine hash() exists as cryptographic, the way to repeat the previous code would be like this:

<?php
echo hash('sha256', 'contraseña');
?>

edf9cf90718610ee7de53c0dcc250739239044de9ba115bb0ca6026c3e4958a5

Important:

The choice of the hash algorithm is a whole issue, md5 is extremely easy and fast to calculate, so brute force attacks are totally feasible, I suggest you point to sha256 up, generate hashes longer and more difficult to solve by brute force. In any case these algorithms are of general purpose and were not specially designed to resolve passwords, if we add that the computational power continues to grow, a hash considered safe today can not be tomorrow. Today, other algorithms are often recommended to treat passwords, I recommend this document .

    
answered by 26.01.2018 в 15:40
0

I do not know that hash function (), to create a md5 hash it would be like this:

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

Greetings

Edit: However to avoid changing too much you could also create or modify that hash function and use hashlib inside, for example:

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

Edit2:

It's true, how Patricio says, I thought of hash as a function defined by the user, it's not a good idea, and in the code I'm missing hexdigest:

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

Thank you very much.

    
answered by 26.01.2018 в 14:41