Python is a dynamic object-oriented programming language that can be used for many kinds of software development. It offers strong support for integration with other languages and tools, comes with extensive standard libraries, and can be learned in a few days. Many Python programmers report substantial productivity gains and feel the language encourages the development of higher quality, more maintainable code.

Tags | |

Python MD5 hasing

Getting the MD5 of a string or file can be done easily inside of python with out any added library's or modules.


To hash a string in python like the word "bob" just use the following code



import haslib

hashlib.md5("bob")



This will return a hash of the word "bob".


A more useful way of using MD5 however is to check the hash of a file. This can be done with the following command.



This example creates a function that you would call:


Example taken from (http://thejaswihr.blogspot.com/2008/06/python-md5-checksum-of-file.html)





#!/usr/local/bin/python


import os, sys

import hashlib


def md5(fileName, excludeLine="", includeLine=""):

    """Compute md5 hash of the specified file"""
    m = hashlib.md5()
    try:
        fd = open(fileName,"rb")
    except IOError:
        print "Unable to open the file in readmode:", filename
        return
    content = fd.readlines()
    fd.close()
    for eachLine in content:
        if excludeLine and eachLine.startswith(excludeLine):
            continue
        m.update(eachLine)
    m.update(includeLine)
    return m.hexdigest()

if __name__ == "__main__":
    for eachFile in sys.argv[1:]:
        print "%s %s" %(md5(eachFile), eachFile)



To use the command you would use the example below where the "file_name.txt" would be replaced with your file.





md5("C:\file_name.txt")



This is good, but I like a minimal code style, here is a way to do this with a little less code:






import hashlib, sys, md5

m = hashlib.md5()

qq=os.listdir("C:\\directory_path\\")

for i in range(0,len(qq)):

    qvar = "C:\\directory_path\\"+qq[i]

    print qvar

    t = file(qvar,"rb").read()

    hexstring = md5.md5(t).hexdigest()

    print hexstring





In my example, first I get a directory listing of all the objects in the folder. Next I loop through that list getting the MD5 value and printing the file name and hash below it.



The one draw back to my example is that it works for folders with just files, but it wont work recursively in sub folders




Comments

Feel free to leave a comment or question

Name: (Optional but appreciated):

Comment:

Use [code] [/code] for code block style



Security Code: