从另一个脚本中匹配MD5哈希 [英] Matching MD5 Hashes from another script

查看:96
本文介绍了从另一个脚本中匹配MD5哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我正在尝试创建一个执行以下操作的脚本:在目录中搜索已知散列。这是我的第一个脚本:

Hash.py

  import hashlib 
$ b $ from functools import partial
$ b $#另一个python脚本
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哈希是:
print(md5sum('photo.jpg'))

如果md5List == md5sum:
printMatch

已知散列.py

  print(Call worked \\\


md5List =01071709f67193b295beb7eab6e66646+ 5d41402abc4b2a76b9719d911017c592

现在的问题是我手动输入我想要的文件找出它所说的photo.jpg的散列。此外,我还没有得到md5List的工作。



我希望脚本最终能够像这样工作:

  python hash.py< directory> 
1匹配
cookies.jpg匹配哈希

所以我怎样才能得到脚本来搜索一个目录而不是手动输入什么文件来散列?此外,如何修复md5List,因为这是错误的?

解决方案

您可以获取当前工作目录中的文件列表使用以下内容。这是您运行脚本的目录。

  import os 

#获取文件列表在工作目录中
files_list = os.listdir(os.getcwd())

您可以使用for循环遍历列表:

  for files_list:
#do something

由于equinoxel也在下面提及,您可以使用 os.walk()


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?

解决方案

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.

这篇关于从另一个脚本中匹配MD5哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆