|
1 | 1 | # -*- coding: utf-8 -*-
|
2 |
| -""" |
3 |
| -Created on Tue Jun 23 21:30:06 2015 |
| 2 | +# |
| 3 | +# WinPython hash.py script |
| 4 | +# Copyright © 2014-2025+ The Winpython development team https://github.com/winpython/ |
| 5 | +# Licensed under the terms of the MIT License |
| 6 | +# (see winpython/__init__.py for details) |
4 | 7 |
|
5 |
| -@author: famille |
6 |
| -""" |
7 |
| - |
8 |
| -import io |
9 |
| -import os |
| 8 | +from pathlib import Path |
10 | 9 | import sys
|
11 | 10 | import hashlib
|
12 | 11 |
|
13 |
| - |
14 |
| -def give_hash(file_in, with_this): |
15 |
| - with io.open(file_in, 'rb') as f: |
16 |
| - return with_this(f.read()).hexdigest() |
17 |
| - |
18 |
| -def give_hashblake(file_in, with_this): |
19 |
| - with io.open(file_in, 'rb') as f: |
20 |
| - return with_this(f.read(),digest_size=32).hexdigest() |
21 |
| - |
22 |
| - |
23 |
| -if __name__ == '__main__': |
24 |
| - if len(sys.argv) < 2: |
25 |
| - print( "Usage: hash.py files_to_copte_hash" ) |
26 |
| - exit(1) |
27 |
| - files = [str(i) for i in sys.argv[1:] if str(i)[-3:].lower() != ".py"] |
28 |
| - |
29 |
| - header = ( |
30 |
| - " MD5" |
31 |
| - + " " * (32 - 4) |
32 |
| - + " | SHA-1" |
33 |
| - + " " * (40 - 5) |
34 |
| - + " | SHA-256" |
35 |
| - + " " * (64 - 7) |
36 |
| - + " | Binary" |
37 |
| - + " " * (33 - 5) |
38 |
| - + "| Size" |
39 |
| - + " " * (20 - 6) |
40 |
| - #+ " | SHA3-256" |
41 |
| - #+ " " * (64 - 8) |
42 |
| - + " | blake2b-256" |
43 |
| - + " " * (64 - 11) |
44 |
| - ) |
45 |
| - line = "|".join( |
46 |
| - ["-" * len(i) for i in header.split("|")] |
47 |
| - ) |
| 12 | +def compute_hash(file_path, hash_function, digest_size=None): |
| 13 | + """Compute the hash of a file using the specified hash function.""" |
| 14 | + try: |
| 15 | + with open(file_path, 'rb') as file: |
| 16 | + if digest_size: |
| 17 | + return hash_function(file.read(), digest_size=digest_size).hexdigest() |
| 18 | + return hash_function(file.read()).hexdigest() |
| 19 | + except IOError as e: |
| 20 | + print(f"Error reading file {file_path}: {e}") |
| 21 | + return None |
| 22 | + |
| 23 | +def print_hashes(files): |
| 24 | + """Print the hashes of the given files.""" |
| 25 | + header = f"{'MD5':<32} | {'SHA-1':<40} | {'SHA-256':<64} | {'Binary':<33} | {'Size':<20} | {'blake2b-256':<64}" |
| 26 | + line = "|".join(["-" * len(part) for part in header.split("|")]) |
48 | 27 |
|
49 | 28 | print(header)
|
50 | 29 | print(line)
|
51 | 30 |
|
52 | 31 | for file in files:
|
53 |
| - print(""+ |
54 |
| - f"{give_hash(file, hashlib.md5)} | " + |
55 |
| - f"{give_hash(file, hashlib.sha1)} | " + |
56 |
| - f"{give_hash(file, hashlib.sha256)} | " + |
57 |
| - f"{os.path.basename(file):33} |"+ |
58 |
| - f"{os.path.getsize(file):13,}".replace(",", " ") + ' Bytes | ' + |
59 |
| - # f" | {give_hash(file, hashlib.sha3_256)}" |
60 |
| - f"{give_hashblake(file, hashlib.blake2b)}") |
| 32 | + md5 = compute_hash(file, hashlib.md5) |
| 33 | + sha1 = compute_hash(file, hashlib.sha1) |
| 34 | + sha256 = compute_hash(file, hashlib.sha256) |
| 35 | + name = Path(file).name.ljust(33) |
| 36 | + size = f"{Path(file).stat().st_size:,} Bytes".replace(",", " ").ljust(20) |
| 37 | + blake2b = compute_hash(file, hashlib.blake2b, digest_size=32) |
| 38 | + print(f"{md5} | {sha1} | {sha256} | {name} | {size} | {blake2b}") |
61 | 39 |
|
62 |
| - |
63 |
| - |
| 40 | +if __name__ == '__main__': |
| 41 | + if len(sys.argv) < 2: |
| 42 | + print("Usage: hash.py files_to_compute_hash") |
| 43 | + sys.exit(1) |
| 44 | + files = [file for file in sys.argv[1:] if file[-3:].lower() != ".py"] |
| 45 | + print_hashes(files) |
0 commit comments