-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhashcalc.py
92 lines (75 loc) · 2.75 KB
/
hashcalc.py
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
import logging
import os
from hashlib import md5, sha1, sha224, sha384, sha256, sha512
from PySide2.QtCore import QThread, Signal, QObject
from PySide2.QtWidgets import QLineEdit
logging.basicConfig(level=logging.DEBUG,
format='%(name)s - %(levelname)s : %(message)s')
class HashingMethods(QThread):
__BLOCKSIZE: int = 4096
__fileLoc: str
__lineEditHashBox: QLineEdit
__hashName: str
__termination: bool
class __SignalEmitter(QObject):
calculatedHash = Signal(str)
progressBarValue = Signal(int)
signalEmitter = __SignalEmitter()
def run(self):
self.__hashCalculate()
def setBLOCKSIZE(self, blocksize: int):
self.__BLOCKSIZE = blocksize
def setFileLoc(self, fileLoc: str):
self.__fileLoc = fileLoc
def setHashName(self, hashName: str):
self.__hashName = hashName.lower()
def getHash(self):
return self.__calculatedHash
def __hashDecider(self):
if self.__hashName == 'md5':
return md5()
elif self.__hashName == 'sha1':
return sha1()
elif self.__hashName == 'sha224':
return sha224()
elif self.__hashName == 'sha256':
return sha256()
elif self.__hashName == 'sha384':
return sha384()
elif self.__hashName == 'sha512':
return sha512()
def terminateThread(self):
if self.isRunning():
self.__termination = True
def __hashCalculate(self):
count: int = 0
sizeCount: int = 0
fileSize = os.stat(self.__fileLoc).st_size
perUnit = fileSize / 100
hasher = self.__hashDecider()
self.__termination = False
with open(self.__fileLoc, 'rb') as file:
fileData = file.read(self.__BLOCKSIZE)
while fileData:
sizeCount += self.__BLOCKSIZE
if sizeCount >= perUnit:
sizeCount -= perUnit
count += 1
# noinspection PyUnresolvedReferences
self.signalEmitter.progressBarValue.emit(count)
hasher.update(fileData)
fileData = file.read(self.__BLOCKSIZE)
if self.__termination:
file.close()
return
else:
count = 100
# noinspection PyUnresolvedReferences
self.signalEmitter.progressBarValue.emit(count)
calculatedHash: str = hasher.hexdigest()
logging.info('Response from the thread: ' + calculatedHash)
# noinspection PyUnresolvedReferences
self.signalEmitter.calculatedHash.emit(calculatedHash)
return
if __name__ == '__main__':
print('Hello World')