wkcqll
wkcqll

Reputation: 1

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

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

Upvotes: 0

Views: 219

Answers (1)

user11544535
user11544535

Reputation:

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'

Upvotes: 1

Related Questions