-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPySha.py
More file actions
155 lines (110 loc) · 3.99 KB
/
PySha.py
File metadata and controls
155 lines (110 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# (c) Martin Ritter, 2020
# ---------- Import Dependencies
#
#
import os
import sys
import hashlib
import itertools
import math
from tkinter import filedialog, Tk
from pathlib import Path
# ---------- Global variables
#
#
version = "1.1"
Buf_size = 1024*1024 # 1024 = 1KB ... * 1024 = 1MB ...
# ---------- Print box
#
#
def printbox (layout, width, height, box_halign, box_valign, content, c_halign, c_valign):
terminalsize = os.get_terminal_size()
box = []
i = 0
cwidth = width-2
content_height = len(content.splitlines())
if c_valign == "top":
cheight_upper = 0
cheight_lower = height - content_height
elif c_valign == "bottom":
cheight_upper = height - content_height
cheight_lower = 0
elif c_valign == "center":
cheight_upper = math.floor((height - content_height)/2)
cheight_lower = math.ceil((height - content_height)/2)
box.append(layout[0] + cwidth*layout[4] + layout[1])
while cheight_upper > 0:
box.append(layout[5] + cwidth*layout[6] + layout[5])
cheight_upper -= 1
for line in content.splitlines():
if line == "":
box.append(layout[5] + cwidth*layout[6] + layout[5])
elif c_halign == "left":
box.append(layout[5] + line.ljust(cwidth) + layout[5])
elif c_halign == "right":
box.append(layout[5] + line.rjust(cwidth) + layout[5])
elif c_halign == "center":
box.append(layout[5] + line.center(cwidth) + layout[5])
while cheight_lower > 0:
box.append(layout[5] + cwidth*layout[6] + layout[5])
cheight_lower -= 1
box.append(layout[2] + cwidth*layout[4] + layout[3])
if box_valign == "top":
printer_h = 0
printer_l = (terminalsize[1]-2) - (height + 2)
elif box_valign == "bottom":
printer_h = (terminalsize[1]-2) - (height + 2)
printer_l = 0
elif box_valign == "center":
printer_h = math.floor(((terminalsize[1]-2) - (height+2))/2)
printer_l = math.ceil(((terminalsize[1]-2) - (height+2))/2)
while printer_h > 0:
print ("")
printer_h -= 1
for entry in box:
if box_halign == "left":
print(entry.ljust(terminalsize[0]))
elif box_halign == "right":
print(entry.rjust(terminalsize[0]))
elif box_halign == "center":
print(entry.center(terminalsize[0]))
while printer_l > 0:
print ("")
printer_l -= 1
# ========================================================================================================================
os.system("cls")
starttext = """\
* * * PySHA {version} * * *
Your friendly Python SHA3-512 Hasher.
""".format(version = version)
printbox("╭╮╰╯─│ ", 50, 8, "center", "top", starttext, "center", "center")
Tk().withdraw()
startpath = Path("\\\\?\\" + filedialog.askdirectory(title = "Select folder"))
filesdone = 0
hashes = []
print ("Hashing recursivly from " + str(startpath))
print ("Counting all files...", end = "\t")
sys.stdout.flush
allfiles = str(f"{(sum(len(files) for _, _, files in os.walk(startpath))):,}")
print (allfiles + " files found!\n")
for root, dirs, files in os.walk(startpath):
for filename in files:
filepath = Path(root).joinpath(filename)
#filesize = str("{:.2f}".format(os.path.getsize(filepath)/(1024**3)))
filesize = str("{:.2f}".format((filepath.stat().st_size)/(1024**3)))
file_hash = hashlib.sha3_512()
filesdone += 1
print ("Processing file " + str(f"{filesdone:,}") + " of " + allfiles)
with open(filepath, "rb") as file:
for i in itertools.count():
data = file.read(Buf_size)
currentsize = str("{:.2f}".format(i*(Buf_size/(1024**3)),2))
print ("Reading " + currentsize + "GB of " + filesize + "GB from " + filename, end="\r")
if not data:
break
file_hash.update(data)
print ("\n")
hashes.append(file_hash.hexdigest() + " *" + str(filepath.relative_to(startpath)))
with open(os.path.join(startpath, "checksums.sha3-512"), "w+", encoding="utf-8") as file:
for hash in hashes:
file.write(hash + "\n")