Is there a python function for getting the same result to php.hash?

0

I want my Python crc32 function get the same result with PHP hash function. Where is the Python module in this world? My heart is almost collapsed at this moment.

The PHP function is:

hexdec(hash('crc32', 'hi', false))

The Python function I used:

binascii.crc32('hi') & 0xffffffff

PHP:

<?php
function_exists('abs');
function_exists('hexdec');
function_exists('hash');
$hash = hexdec(hash('crc32', 'hi', false));
echo $hash. "\n";
?>

Output:

4049932203

Python:

import binascii
binascii.crc32('hi') & 0xffffffff

Output:

3633523372

php
python
hash
asked on Stack Overflow May 23, 2019 by wkcqll • edited May 23, 2019 by cabrerahector

1 Answer

1

Make sure to use the same hash algorithm. For example:

PHP

php > echo hash('sha512', 'foo');

Result:

f7fbba6e0636f890e56fbbf3283e524c6fa3204ae298382d624741d0dc6638326e282c41be5e4254d8820772c5518a2c5a8c0c7f7eda19594a7eb539453e1ed7

Python

import hashlib hashlib.sha512(b'foo').hexdigest()

Result: f7fbba6e0636f890e56fbbf3283e524c6fa3204ae298382d624741d0dc6638326e282c41be5e4254d8820772c5518a2c5a8c0c7f7eda19594a7eb539453e1ed7'

answered on Stack Overflow May 23, 2019 by Talaat Magdy

User contributions licensed under CC BY-SA 3.0