|
| 1 | +import argparse |
| 2 | +from filehash import FileHash |
| 3 | +import magic |
| 4 | +import os |
| 5 | + |
| 6 | + |
| 7 | +def file_type_hashes(files): |
| 8 | + # Declare the type of hash you want |
| 9 | + md5Hash = FileHash('md5') |
| 10 | + sha1Hash = FileHash('sha1') |
| 11 | + sha256Hash = FileHash('sha256') |
| 12 | + sha512Hash = FileHash('sha512') |
| 13 | + crcHash = FileHash('crc32') |
| 14 | + adler32Hash = FileHash('adler32') |
| 15 | + |
| 16 | + # Iterate through the files |
| 17 | + for file in files: |
| 18 | + |
| 19 | + # Check if file exists |
| 20 | + if os.path.isfile(file): |
| 21 | + |
| 22 | + # Generate the file hash and the file type as well |
| 23 | + file_info = { |
| 24 | + "filename": file, |
| 25 | + "filetype": magic.from_file(file), |
| 26 | + "md5": md5Hash.hash_file(file), |
| 27 | + "sha1": sha1Hash.hash_file(file), |
| 28 | + "sha256": sha256Hash.hash_file(file), |
| 29 | + "sha512": sha512Hash.hash_file(file), |
| 30 | + "crc32": crcHash.hash_file(file), |
| 31 | + "adler32": adler32Hash.hash_file(file) |
| 32 | + } |
| 33 | + |
| 34 | + print(file_info) |
| 35 | + |
| 36 | + else: |
| 37 | + print(f"No File Found: {file}") |
| 38 | + |
| 39 | + |
| 40 | +def file_type(files): |
| 41 | + |
| 42 | + # Iterate through the files |
| 43 | + for file in files: |
| 44 | + |
| 45 | + # Check if file exists |
| 46 | + if os.path.isfile(file): |
| 47 | + |
| 48 | + # Generate the file type |
| 49 | + print(f"Filename: {file} , File Type: {magic.from_file(file)}") |
| 50 | + |
| 51 | + else: |
| 52 | + print(f"No File Found: {file}") |
| 53 | + |
| 54 | + |
| 55 | +def main(): |
| 56 | + parser = argparse.ArgumentParser(description='Get The Hashes And File Type Of File(s)') |
| 57 | + |
| 58 | + # Argument is to accept a file(s) |
| 59 | + parser.add_argument('-f', dest='file', nargs='+', type=str, help='Submit file(s)', required=True) |
| 60 | + |
| 61 | + # Argument is to display only the file type of file(s) |
| 62 | + parser.add_argument('-t', dest='type', action='store_true', help='Get only the file type of file(s)') |
| 63 | + |
| 64 | + args = parser.parse_args() |
| 65 | + |
| 66 | + if args.file: |
| 67 | + # Accepts files |
| 68 | + files = args.file |
| 69 | + |
| 70 | + if files: |
| 71 | + # Display the file type only |
| 72 | + if args.type: |
| 73 | + file_type(files) |
| 74 | + |
| 75 | + # Show the hashes and the file type as well |
| 76 | + else: |
| 77 | + file_type_hashes(files) |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == '__main__': |
| 81 | + main() |
0 commit comments