From 975e60710e5c0f6c4f1f484d40e0adb5b17a9079 Mon Sep 17 00:00:00 2001 From: CodeZeroNull Date: Mon, 23 Sep 2024 14:33:30 +0200 Subject: [PATCH 1/4] changed to function and removed harcoded input and output --- hasTar.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/hasTar.py b/hasTar.py index 6fee680..cb96dac 100644 --- a/hasTar.py +++ b/hasTar.py @@ -1,15 +1,26 @@ import tarfile import hashlib +""" +This script calculates hashes for every file inside a tarfile and creates a checksum file +Usage + +""" + + 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: + outputname = input_tar_file + 'sha1' + 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)) + + From 5455a17996476c820b68d7adedba35022a23ffb1 Mon Sep 17 00:00:00 2001 From: CodeZeroNull Date: Mon, 23 Sep 2024 14:36:52 +0200 Subject: [PATCH 2/4] added minimal documention and hability to run as script --- hasTar.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/hasTar.py b/hasTar.py index cb96dac..bd2790d 100644 --- a/hasTar.py +++ b/hasTar.py @@ -2,8 +2,9 @@ import hashlib """ -This script calculates hashes for every file inside a tarfile and creates a checksum file -Usage +This script calculates hashes for every file inside a tar file and creates a checksum file +Usage: +python3 hashTar.py target-tar-file """ @@ -23,4 +24,5 @@ def hashtar(input_tar_file): with tar_input.extractfile(member) as _file: checksums_file.write('{} ./{}\n'.format(checksum(_file), member.name)) - +if __name__ == '__main__': + hashtar(sys.argv[1]) From d2c2a817e0f55eb98841fa2c666d1450cfd7ff40 Mon Sep 17 00:00:00 2001 From: CodeZeroNull Date: Mon, 23 Sep 2024 14:41:04 +0200 Subject: [PATCH 3/4] added sys, added '.' before sha1 extension of output file --- hasTar.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/hasTar.py b/hasTar.py index bd2790d..2834817 100644 --- a/hasTar.py +++ b/hasTar.py @@ -1,3 +1,4 @@ +import sys import tarfile import hashlib @@ -17,7 +18,8 @@ def checksum(file_to_hash): def hashtar(input_tar_file): with tarfile.open(input_tar_file) as tar_input: - outputname = input_tar_file + 'sha1' + 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) From 1241e253d38d4c37a20261557c555e7943d14b59 Mon Sep 17 00:00:00 2001 From: CodeZeroNull Date: Mon, 23 Sep 2024 14:48:31 +0200 Subject: [PATCH 4/4] renamed file --- hasTar.py => hashTar.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename hasTar.py => hashTar.py (100%) diff --git a/hasTar.py b/hashTar.py similarity index 100% rename from hasTar.py rename to hashTar.py