I am rewriting an authentication library written in PHP in Python. This is all outdated code; the original developers have long passed. They used the PHP 'pack' command to convert the string to hex using the "H" flag. The PHP documentation describes this as "Hex string, high nibble first". I read another question (the Python equivalent of the php package ) that suggested using binascii.unhexlify (), but that complains whenever I pass a non-hexadecimal character.
So my question is what does the PHP pack function do with characters without hexadecimal characters? Throws them away, or there is an additional step that performs the translation. Is there a better way in Python than binascii.unhexlify?
So the package 'H *'
php -r 'print pack("H*", md5("Dummy String"));'
Returns
??????=?PW??
In python:
secret = binascii.unhexlify( "Dummy String" ) TypeError: Non-hexadecimal digit found
Thanks for the help.
[EDIT]
So DJV was fundamentally right. At first I needed to convert the value to md5, however this is interesting. In python, the md5 library returns binary data using the digest method.
In my case, I could skip all binascii calls and just use
md5.md5('Dummy String').digest()
What is the same in PHP like:
pack ("H *", md5 ("Dummy String"));
Funny stuff. Good to know.
source share