nanopyx.core.io.checksum
1import hashlib 2 3 4def get_checksum(file_path: str) -> str: 5 """ 6 Returns the SHA-256 checksum of the file 7 :param file_path: path to the file 8 :type file_path: str 9 :return: checksum in hexadecimal format 10 :rtype: str 11 """ 12 13 # Open the file in binary mode 14 with open(file_path, "rb") as file: 15 # Create a SHA-256 hash object 16 hash_object = hashlib.sha256() 17 18 # Iterate over the file in chunks 19 for chunk in iter(lambda: file.read(4096), b""): 20 # Feed the chunk to the hash object 21 hash_object.update(chunk) 22 23 # Obtain the checksum in hexadecimal format 24 checksum = hash_object.hexdigest() 25 26 return checksum
def
get_checksum(file_path: str) -> str:
5def get_checksum(file_path: str) -> str: 6 """ 7 Returns the SHA-256 checksum of the file 8 :param file_path: path to the file 9 :type file_path: str 10 :return: checksum in hexadecimal format 11 :rtype: str 12 """ 13 14 # Open the file in binary mode 15 with open(file_path, "rb") as file: 16 # Create a SHA-256 hash object 17 hash_object = hashlib.sha256() 18 19 # Iterate over the file in chunks 20 for chunk in iter(lambda: file.read(4096), b""): 21 # Feed the chunk to the hash object 22 hash_object.update(chunk) 23 24 # Obtain the checksum in hexadecimal format 25 checksum = hash_object.hexdigest() 26 27 return checksum
Returns the SHA-256 checksum of the file
Parameters
- file_path: path to the file
Returns
checksum in hexadecimal format