Matching MD5 Hashes from another script

时光总嘲笑我的痴心妄想 提交于 2019-12-12 04:38:20

问题


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?


回答1:


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.




回答2:


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)]


来源:https://stackoverflow.com/questions/17428629/matching-md5-hashes-from-another-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!