-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from CodeZeroNull/code0/pick-target
Code0/pick target
- Loading branch information
Showing
1 changed file
with
21 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |