我不想重新創建可能已經存在的模塊。但對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