'How can I get rmd160 digest of file in python (without subprocess)?

I know I can get the sha256 digest of a given file using hashlib.sha256, and could get the rmd160 digest using a subprocess call to openssl rmd160 <myfile>, but is there a python package I can import that provides a method to determine the rmd160 digest?

(rmd160 is a recommended checksum for use in a Macports Portfile [1].)



Solution 1:[1]

It's not included in the standard library, but pycrypto supports it.

If you're using a unix system with access to a compiler and the required dependencies, you can simply pip install pycrypto. If you're using windows, there are third party pre-built binaries

Solution 2:[2]

You may use hashlib.new(name) to create hashers from the algorithm name. My understanding of the documentation (both Python 2 and 3) is that in this case this is delegated to OpenSSL. OpenSSL supports the names "rmd160" and "ripemd160".

I use

def rmd160(fname):
    BLOCKSIZE = 65536
    h = hashlib.new("rmd160")
    with open(fname, 'rb') as f:
        buf = f.read(BLOCKSIZE)
        while buf:
            h.update(buf)
            buf = f.read(BLOCKSIZE)
    return h.hexdigest()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 loopbackbee
Solution 2