编程语言
首页 > 编程语言> > python3求取大文件sha1值

python3求取大文件sha1值

作者:互联网

小文件

import hashlib
import base64

filePath = "test.txt"
with open(filePath, "rb") as f:
    fileData = f.read()
sha1 = hashlib.sha1()
sha1.update(fileData)
fileHash = base64.b64encode(sha1.digest()).decode('utf-8 ')
print(fileHash)
print(base64.b64decode(fileHash).hex())

在linux中,可以使用指令sha1sum test.txt验证

大文件

import hashlib
import base64

filePath = "test.txt"
sha1 = hashlib.sha1()
with open(filePath, "rb") as f:
    while True:
        fileData = f.read(2048)
        if not fileData:
            break
        sha1.update(fileData)
fileHash = base64.b64encode(sha1.digest()).decode('utf-8 ')
print(fileHash)
print(base64.b64decode(fileHash).hex())

参考: python 计算大文件的md5、sha1值

标签:sha1,fileHash,base64,求取,print,import,fileData,python3
来源: https://www.cnblogs.com/brian-sun/p/15508161.html