1 year ago
#301406
skimchi
Getting SHA of files inside ISO with python
I have an ISO file that I'm iterating through with python's pycdlib (as described here https://clalancette.github.io/pycdlib/example-walking-iso-filesystem.html ) And I want to put each file /directory name into a list, with its SHA calculation. The sha function works on a local file but doesn't work inside the ISO. I tried putting the exact path to each file but it shouts FileNotFoundError
I tried using code from here, but no luck.
Any idea how to make this work?
The sha function:
def sha_file(filename):
# Python program to find SHA256 hash string of a file
# make a hash object
h = hashlib.sha256()
# 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()
The get files function:
def get_all_ISO_filenames(isoFilesPath):
iso = pycdlib.PyCdlib()
iso.open(isoFilesPath)
fileOrFolderObjects = []
for dirname, dirlist, filelist in iso.walk(rr_path='/'):
if filelist:
for i in filelist:
print(i)
sha_of_file = sha_file(dirname + "/" + i)
objectList = [i, sha_of_file, dirname]
fileOrFolderObjects.append(objectList)
elif dirlist:
for j in dirlist:
print(j)
# sha_of_file = sha_file("/" + j)
objectList = [j, "", dirname]
fileOrFolderObjects.append(objectList)
# print("Dirname:", dirname, ", Dirlist:", dirlist, ", Filelist:", filelist)
iso.close()
print(fileOrFolderObjects)
get_all_ISO_filenames(iso_path)
python
sha
iso
0 Answers
Your Answer