Instead of loading the whole image into the memory when generating the
sha256 sum, we load the file in chunks and update the hash incrementally
to avoid MemoryError in python. Also remove a stray empty line.
Fixes: #13056
Signed-off-by: Adones Pitogo <pitogo.adones@gmail.com>
(mention empty line removal, adds Fixes from PR)
Signed-off-by: Christian Lamparter <chunkeey@gmail.com>
json_path = Path(argv[1])
file_path = Path(getenv("FILE_DIR")) / getenv("FILE_NAME")
json_path = Path(argv[1])
file_path = Path(getenv("FILE_DIR")) / getenv("FILE_NAME")
if not file_path.is_file():
print("Skip JSON creation for non existing file", file_path)
exit(0)
if not file_path.is_file():
print("Skip JSON creation for non existing file", file_path)
exit(0)
device_id = getenv("DEVICE_ID")
device_id = getenv("DEVICE_ID")
-hash_file = hashlib.sha256(file_path.read_bytes()).hexdigest()
+
+sha256_hash = hashlib.sha256()
+with open(str(file_path),"rb") as f:
+ # Read and update hash string value in blocks of 4K
+ for byte_block in iter(lambda: f.read(4096),b""):
+ sha256_hash.update(byte_block)
+
+hash_file = sha256_hash.hexdigest()
if file_path.with_suffix(file_path.suffix + ".sha256sum").exists():
hash_unsigned = (
if file_path.with_suffix(file_path.suffix + ".sha256sum").exists():
hash_unsigned = (