我正在编写 python (2.7) 脚本来比较两个列表。这些列表是通过读取文件内容从文件创建的。文件只是文本文件,没有二进制文件。文件 1 仅包含哈希值(某些明文单词的 MD5 和),文件 2 是 hash:plain。列表具有不同的长度(从逻辑上讲,我可以拥有比散列更少的“破解”条目),并且两者都无法排序,因为我必须保留顺序,但这是我想要实现的下一步。到目前为止,我的简单代码如下所示:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
def ifexists(fname):
if not os.path.isfile(fname):
print('[-] %s must exist' % fname)
sys.exit(1)
if len(sys.argv) < 2:
print('[-] please provide CRACKED and HASHES files')
sys.exit(1)
CRACKED=sys.argv[1]
HASHES=sys.argv[2]
sk_ifexists(CRACKED)
sk_ifexists(HASHES)
with open(CRACKED) as cracked, open(HASHES) as hashes:
hashdata=hashes.readlines()
crackdata=cracked.readlines()
for c in crackdata:
for z in hashdata:
if c.strip().split(':', 1)[0] in z:
print('found: ', c.strip().split(':', 1))
基本上,我必须将 HASHES 列表中找到的哈希替换为 CRACKED 列表中找到的匹配行 hash:plain 。我正在迭代 CRACKED,因为它每次都会更短。所以我的问题是上面的代码对于较长的列表来说非常慢。例如,处理两个 60k 行的文本文件最多需要 15 分钟。您有什么建议可以加快速度?
将这些文件之一存储在字典或集合中;这会完成一个完整的循环,并且查找平均时间为 O(1) 常量。
例如,它看起来像crackdata
文件可以轻松转换为字典:
with open(CRACKED) as crackedfile:
cracked = dict(map(str.strip, line.split(':')) for line in crackedfile if ':' in line)
现在你只需要循环另一个文件once:
with open(HASHES) as hashes:
for line in hashes:
hash = line.strip()
if hash in cracked:
print('Found:', hash, 'which maps to', cracked[hash])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)