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

cabrerahector
  • 3,653
  • 4
  • 16
  • 27
wkcqll
  • 1
  • 1
  • CRC is a family of algorithms that work in the same way. They get input data, and they use a second value - 32 bits in case of CRC32 - that tells them how to do the checksumming. Looks like the defaults for the second value are different in PHP and Python. – Roland Weber May 23 '19 at 13:09
  • Possible duplicate of https://stackoverflow.com/questions/30092226/how-to-calculate-crc32-with-python-to-match-online-results – Roland Weber May 23 '19 at 13:11
  • Possible duplicate of [How to calculate CRC32 with Python to match online results?](https://stackoverflow.com/questions/30092226/how-to-calculate-crc32-with-python-to-match-online-results) – Roland Weber May 23 '19 at 13:11
  • "My heart is almost collapsed at this moment." Ok, relax. The PHP crc32 hash is little different, try using "crc32b" in PHP instead, .e.g. `hexdec(hash('crc32b', 'hi', false))` – President James K. Polk May 23 '19 at 20:56

1 Answers1

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'