Skip to content

Commit

Permalink
Merge pull request #4 from CodeZeroNull/code0/pick-target
Browse files Browse the repository at this point in the history
Code0/pick target
  • Loading branch information
CodeZeroNull authored Sep 23, 2024
2 parents da3f920 + 7d99bbc commit c0640b0
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions hashTar.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
import sys
import tarfile
import hashlib

"""
This script calculates hashes for every file inside a tar file and creates a checksum file
Usage:
python3 hashTar.py target-tar-file
"""


def checksum(file_to_hash):
hashresult = hashlib.sha1()
for chunk in iter(lambda: file_to_hash.read(4096), b''):
hashresult.update(chunk)
return hashresult.hexdigest()

with tarfile.open('./test.tar') as tar_input:
with open('test.tar.sha1', 'w') as checksums_file:
for member in tar_input.getmembers():
if member.isreg(): # skip if not file (folders are members, hashing them fails)
with tar_input.extractfile(member) as _file:
checksums_file.write('{} ./{}\n'.format(checksum(_file), member.name))
def hashtar(input_tar_file):
with tarfile.open(input_tar_file) as tar_input:
algo = "sha1" # Getting ready for algorithm chooser
outputname = input_tar_file + '.' + algo
with open(outputname, 'w') as checksums_file:
for member in tar_input.getmembers():
if member.isreg(): # skip if not file (folders are members, hashing them fails)
with tar_input.extractfile(member) as _file:
checksums_file.write('{} ./{}\n'.format(checksum(_file), member.name))

if __name__ == '__main__':
hashtar(sys.argv[1])

0 comments on commit c0640b0

Please sign in to comment.