python 批量生成随机字符串的hash值
需求
- 由于测试需要,需产生大量SHA1序列,通过生成随机定长序列,然后调用SHA1计算并写入文件。
主要模块
代码实现
#To generate hash.txt
#Author:RobotGF
import hashlib
from random import Random
def random_str():
randomlength=6
str = ''
# chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
chars='abcdefghijklmnopqrstuvwxyz0123456'
length = len(chars) - 1
random = Random()
for i in range(randomlength):
str+=chars[random.randint(0, length)]
return str
hash=open("hash.txt","w+")
plain_hash=open("plain_hash.txt","w+")
num=10000
i=0
while i < num :
str=random_str()
sha1=hashlib.sha1(str)
sha1_hex=sha1.hexdigest()
str=sha1_hex+":"+str+"\n"
sha1_hex += "\n"
plain_hash.write(str)
hash.write(sha1_hex)
i += 1
hash.close()
plain_hash.close()