我正在编写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))
基本上,我必须用在CRACKED列表中找到的匹配行hash:plain替换HASHES列表中找到的哈希。我正在遍历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)
现在你只需要遍历所有的其他文件一旦:
with open(HASHES) as hashes:
for line in hashes:
hash = line.strip()
if hash in cracked:
print('Found:', hash, 'which maps to', cracked[hash])
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句