我不想重新创建可能已经存在的模块。但对programiz解释如何得到一个很好的例子了SHA-1的消息摘要用一种方法获取二进制摘要密钥的Python哈希模块
# Python rogram to find the SHA-1 message digest of a file
# import hashlib module
import hashlib
def hash_file(filename):
""""This function returns the SHA-1 hash
of the file passed into it"""
# make a hash object
h = hashlib.sha1()
# open file for reading in binary mode
with open(filename,'rb') as file:
# loop till the end of the file
chunk = 0
while chunk != b'':
# read only 1024 bytes at a time
chunk = file.read(1024)
h.update(chunk)
# return the hex representation of digest
return h.hexdigest()
message = hash_file("track1.mp3")
print(message)
现在我刚刚创建了一个.py
我进口,但想知道如果这样的方法hashlib
模块或另一个已经存在维护良好的模块?
所以我只能去
import some_hashlib_module
print some_hashlib_module.get_binary_SH1_digest("File of interest")
兼容它可能是有意义的传递文件类对象的函数,而不是文件名,例如,[为远程tarball中的所有文件计算散列值](http://stackoverflow.com/a/27606823/4279)。 – jfs