I am using Django to make an e-shop. I am trying to add online payment with http://liqpay.com/. They have examples in PHP: https://liqpay.com/?do=pages&p=cnb12.
In the example they use PHP's shal($data, 1)
flag 1 (then the sha1 digest is instead returned in raw binary format with a length of 20).
But the Python function hashlib.sha1()
, does not have this flag.
Is there some analog in Python?
Python hashlib objects can provide you with either the raw bytes or a hex digest, depending on the method used. Calling .digest()
gives you raw bytes:
>>> import hashlib
>>> data = 'foobar'
>>> hashlib.sha1(data).digest()
'\x88C\xd7\xf9$\x16!\x1d\xe9\xeb\xb9c\xffL\xe2\x81%\x93(x'
>>> len(hashlib.sha1(data).digest())
20
If you want to get the hex digest, use the .hexdigest()
method instead:
>>> hashlib.sha1(data).hexdigest()
'8843d7f92416211de9ebb963ff4ce28125932878'
In php
$hash = hash("sha1", "something", true)
echo base64_encode($hash)
'GvF+c3IdvgxAARuC7Uuxp9vjzik='
In python
from hashlib import sha1
sha1("something").digest().encode("base64")
'GvF+c3IdvgxAARuC7Uuxp9vjzik= '
Explanation
In php the hash function third param implies that the output will be in binary mode to make the same thing in python you have to use the method digest to get the same thing.