【发布时间】:2020-12-19 04:09:36
【问题描述】:
我是编码初学者,我正在尝试构建一个脚本,该脚本将 txt 文件作为输入,对其进行哈希处理并输出到另一个 txt 文件,该文件的每一行都包含“string:hashedstring”。代码工作正常。我现在面临的问题是,如果输入文件很大,它将消耗所有 RAM 并杀死它。我尝试使用块,但不知道如何将它与多行输入和输出一起使用。 非常欢迎任何关于此处主要主题以外的代码其他部分的建议,因为我才刚刚开始。谢谢。
import argparse
import hashlib
import os
import sys
def sofia_hash(msg):
h = ""
m = hashlib.md5()
m.update(msg.encode('utf-8'))
msg_md5 = m.digest()
for i in range(8):
n = (msg_md5[2*i] + msg_md5[2*i+1]) % 0x3e
if n > 9:
if n > 35:
n += 61
else:
n += 55
else:
n += 0x30
h += chr(n)
return h
top_parser = argparse.ArgumentParser(description='Sofiamass')
top_parser.add_argument('input', action="store", type=argparse.FileType('r', encoding='utf8'), help="Set input file")
top_parser.add_argument('output', action="store", help="Set output file")
args = top_parser.parse_args()
sofiainput = args.input.read().splitlines()
a = 0
try:
while a < len(sofiainput):
target_sofiainput = sofiainput[a]
etarget_sofiainput = (target_sofiainput).encode('utf-8')
try:
sofia_pass = sofia_hash(target_sofiainput)
x = True
except KeyboardInterrupt:
print ("\n[---]exiting now[---]")
if x == True:
with open(args.output, 'a') as sofiaoutput:
sofiaoutput.write(str(target_sofiainput) + ":" + str(sofia_pass) + "\n")
elif x == False:
print('error')
a += 1
except KeyboardInterrupt:
print ("\n[---]exiting now[---]")
except AttributeError:
pass
【问题讨论】:
标签: python python-3.x input hash output