How do you reencode a python string via Packing and Unpacking using binascii?
Asked Answered
A

2

1

I have a file hashed to a standard md5 Hash

Correction: OpenSSL Hashes are HEXDECIMAL representations.

MD5 Hash:   57ED2E029BF9CA39383D2A671EF4FB50

I have a program that requires a byte encoding base64 md5 hash.

MD5 BASE64 byte: 8se7isyX+S6Yei1Ah9AhsQ==

How do you use pythons 'binascii' b2a methods to convert the standard MD5 hash to a Base64?

The below is wrong.

import binascii 
bin = binascii.a2b_uu('57ED2E029BF9CA39383D2A671EF4FB50')
base = binascii.b2a_base64(bin) 

Output +> 'QUlZTlYUmikRYUjRXkQjWQkiiSkmkhZmVjhZkWTAAAAA\n'

TL;DR:

I need to take hexstring, convert it to binary, and then base64 the binary.. what's the python way?

Reference:

Atonality answered 9/12, 2014 at 16:19 Comment(7)
how do you you hash the file?Folger
openssl md5 file.exeAtonality
The other one is a byte representation of the md5 string encoded to base64 rather than asciiAtonality
You can convert a hexadecimal number in a string to a binary value with int('57ED2E029BF9CA39383D2A671EF4FB50', base=16).Nobie
>>> hashlib.md5(open("file.exe").read()).hexdigest() '4bd2f7940a1ec86efe1d1178b4cb23b7'Atonality
Are you really calling binascii.a2b_uu rather than binascii.a2b_hex? The former gives an error about "Trailing Garbage" when I try it with your example hex string, and it's wrong anyway (Uuencode is not the same thing as hex encoding). When I properly decode your hex string and re-encode it to base64, I get b'V+0uApv5yjk4PSpnHvT7UA==\n'.Pleiades
@Nobie that generates a single integer rather than a string of bytes.Mongrelize
M
1

Here's how you can do the conversion with binascii. It requires two conversions, one from hex to binary and another from binary to base64.

>>> hex_hash = '4bd2f7940a1ec86efe1d1178b4cb23b7'
>>> binascii.b2a_base64(binascii.a2b_hex(hex_hash))
'S9L3lAoeyG7+HRF4tMsjtw==\n'
Mongrelize answered 9/12, 2014 at 20:25 Comment(1)
Giving the answer to you, as you used the explict library I mentioned in the question.Atonality
A
1

The most important thing to realize was that the openssl md5 hash is calculated the same way as the hashlib.md5(..).hexdigest() method

import base64
import hashlib    
hex_hash = hashlib.md5(open("putty_upx.exe").read()).hexdigest()
>> '4bd2f7940a1ec86efe1d1178b4cb23b7'
hex_hash.decode("hex")
>> 'K\xd2\xf7\x94\n\x1e\xc8n\xfe\x1d\x11x\xb4\xcb#\xb7'    
b64_md5_hash = base64.b64encode(hex_hash.decode("hex"))
>> 'S9L3lAoeyG7+HRF4tMsjtw=='
len(b64_md5_hash)
>> 24 
Atonality answered 9/12, 2014 at 19:41 Comment(0)
M
1

Here's how you can do the conversion with binascii. It requires two conversions, one from hex to binary and another from binary to base64.

>>> hex_hash = '4bd2f7940a1ec86efe1d1178b4cb23b7'
>>> binascii.b2a_base64(binascii.a2b_hex(hex_hash))
'S9L3lAoeyG7+HRF4tMsjtw==\n'
Mongrelize answered 9/12, 2014 at 20:25 Comment(1)
Giving the answer to you, as you used the explict library I mentioned in the question.Atonality

© 2022 - 2024 — McMap. All rights reserved.