I'm translating an authentication library, written in PHP to Python. It's all legacy code, the original devs long gone. They used PHP's 'pack' command to transform a string into hex using the 'H' flag. PHP's documentation describes this as 'Hex string, high nibble first'. I read another question (Python equivalent of php pack) which suggested using binascii.unhexlify(), but this complains whenever I pass in a non-hex character.
So my question is what does the PHP pack function do with non-hex characters? Does it throw them away, or is there an extra step that performs a translation. Is there a better method in Python than binascii.unhexlify?
So pack'ing '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. I needed to convert the value into md5 first, however that's where it's interesting. In python the md5 library returns binary data via the 'digest' method.
In my case I could skip all the binascii calls and just use
md5.md5('Dummy String').digest()
Which is the same in PHP as:
pack("H*", md5("Dummy String"));
Fun stuff. Good to know.