程式語言 - Python - v3.x - File SHA1



方法一

import sys
import hashlib
 
def get_sha1(path):
    with open(path) as f:
        return hashlib.sha1(f.read().encode('utf-8')).hexdigest()

print('{}'.format(get_sha1(sys.argv[1])))

方法二

import hashlib

sha1 = hashlib.sha1()
with open("file.bin", "rb") as f:
    for chunk in iter(lambda: f.read(8192), b""):
        sha1.update(chunk)

print(sha1.hexdigest())