【问题标题】:Compute SHA1 of Strings in python在python中计算字符串的SHA1
【发布时间】:2014-11-12 04:59:01
【问题描述】:

我有一个包含很多字符串的文件。我正在尝试单独计算这些字符串的 SHA1 哈希并存储它们

import hashlib
inp = open("inp.txt" , "r")
outputhash  = open("outputhashes.txt", "w")
for eachpwd in inp:
    sha_1 = hashlib.sha1()
    sha_1.update(eachpwd)
    outputhash.write(sha_1.hexdigest())
    outputhash.write("\n")

我面临的问题是,一旦计算了字符串 SHA1,就会附加下一个字符串(我觉得这就是为什么我没有得到正确的哈希值)并且正在计算它的哈希值。因此我没有得到正确的哈希值。我是 python 新手。我知道该怎么做,但不知道该怎么做。你能给我指出正确的方向吗?

【问题讨论】:

    标签: python hash sha


    【解决方案1】:

    您正在遍历一个文件,该文件将返回行,包括行终止符(字符串末尾的 \n 字符)

    你应该删除它:

    import hashlib
    inp = open("inp.txt" , "r")
    outputhash  = open("outputhashes.txt", "w")
    for line in inp:            # Change this
        eachpwd = line.strip()  # Change this
    
        # Add this to understand the problem:
        print repr(line)
    
        sha_1 = hashlib.sha1()
        sha_1.update(eachpwd)
        outputhash.write(sha_1.hexdigest())
        outputhash.write("\n")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-17
      • 1970-01-01
      • 2013-11-26
      • 2016-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-14
      相关资源
      最近更新 更多