user1781837 user1781837 - 1 year ago 123
Perl Question

Python equivalent of Perl Digest::MD5 functions

As part of a work project I am porting a Perl library to Python. I'm comfortable with Python, much (much) less so with Perl.

The perl code uses Digest::MD5. This module has three functions:

  • md5($data)
    takes in data and spits out md5 digest in binary

  • md5_hex($data)
    takes in data and spits out md5 digest in hex

  • md5_base64($data)
    takes in data and spits out md5 digest in base64 encoding

I can replicate md5_hex with something like this:

import hashlib
string = 'abcdefg'

Which works fine (same inputs give same outputs at least). I can't seem to get anything to match for the other two functions.

It doesn't help that string encodings are really not something I've done much with. I've been interpreting the perl functions as saying they take an md5 digest and then re-encode in binary or base64, something like this:

import hashlib
import base64
string = 'abcdefg'
md5_string = hashlib.md5(string.encode()).hexdigest()

but maybe that's wrong? I'm sure there's something fundamental I'm just missing.

The Perl doc is here:

Answer Source

The first one would simply be calling .digest method on the md5:

>>> from hashlib import md5
>>> s = 'abcdefg'
>>> md5(s.encode()).digest()

And md5_base64 is the digest but base64-encoded:

>>> base64.b64encode(md5(s.encode()).digest())

However, Perl doesn't return the hash padded, thus to be compatible, you'd strip the = padding characters:

>>> base64.b64encode(md5(s.encode()).digest()).strip(b'=')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download