Matching MD5 Hashes from another script

2019-09-06 17:00发布

Ok so i'm trying to create a script that does the following: Searches a directory for known hashes. Here is my first script:

Hash.py

import hashlib

from functools import partial

#call another python script
execfile("knownHashes.py")

def md5sum(filename):
    with open(filename, mode='rb') as f:
        d = hashlib.md5()
        for buf in iter(partial(f.read, 128), b''):
            d.update(buf)
    return d.hexdigest()
print "Hash of is: "
print(md5sum('photo.jpg'))

if md5List == md5sum:
    print "Match"

knownHashes.py

print ("Call worked\n")

md5List = "01071709f67193b295beb7eab6e66646" + "5d41402abc4b2a76b9719d911017c592"

The problem at the moment is that I manually have to type in the file I want to find out the hash of where it says photo.jpg. Also, The I haven't got the md5List to work yet.

I want the script to eventually work like this:

python hash.py <directory>
1 match 
cookies.jpg matches hash

So how can I get the script to search a directory rather than manually type in what file to hash? Also, how can I fix the md5List because that is wrong?

标签: python hash md5
2条回答
贪生不怕死
2楼-- · 2019-09-06 17:43

You can get a list of files in the current working directory using the following. This is the directory that you run the script from.

import os

#Get list of files in working directory
files_list = os.listdir(os.getcwd())

You can iterate through the list using a for loop:

for file in files_list:
    #do something

As equinoxel also mentioned below, you can use os.walk() as well.

查看更多
Fickle 薄情
3楼-- · 2019-09-06 17:45

Simple little gist should solve most of your problems. Understandable if you don't like using OOP for this problem, but I believe all of the important conceptual pieces are here in a pretty clean, concise representation. Let me know if you have any questions.

class PyGrep:

    def __init__(self, directory):
        self.directory = directory

    def grab_all_files_with_ending(self, file_ending):
        """Will return absolute paths to all files with given file ending in self.directory"""
        walk_results = os.walk(self.directory)
        file_check = lambda walk: len(walk[2]) > 0
        ending_prelim = lambda walk: file_ending in " ".join(walk[2])
        relevant_results = (entry for entry in walk_results if file_check(entry) and ending_prelim(entry))
        return (self.grab_files_from_os_walk(result, file_ending) for result in relevant_results)

    def grab_files_from_os_walk(self, os_walk_tuple, file_ending):
        format_check = lambda file_name: file_ending in file_name
        directory, subfolders, file_paths = os_walk_tuple
        return [os.path.join(directory, file_path) for file_path in file_paths if format_check(file_path)]
查看更多
登录 后发表回答