-
Notifications
You must be signed in to change notification settings - Fork 0
/
checksum_utility.py
198 lines (162 loc) · 9.32 KB
/
checksum_utility.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import sys
import hashlib
import base64
from PyQt5.QtWidgets import (QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QFileDialog, QCheckBox, QMessageBox, QGridLayout)
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtCore import Qt
from io import BytesIO
from PIL import Image
# Base64 encoded icon (example, replace with your actual base64 data)
icon_base64 = """
iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAAsQAAALEBxi1JjQAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAATHSURBVFiFvZd/TJR1HMdfd94PO5x2GSpO8W5OtGaSZRP17gQUaOFAR9PmH9qk3GiVbJj4T9xzpo5INmZZW2ZGbbWZiAr245I44UBTJDMnMf8AsSmWjhBRDpDrj/vBc88PPKT13p49z/P5vL/P5/398fl8vw+MDQLgH82lG6f1AamhD+jGKABnfgpCfkrUfI2lyGCOMf7Q1etLBzxaFZ4eOMLIvRnG48th1ruBexQ4vjFdb44xuoFUt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l0bcb30HEf3ZTE05OflLZsoOnGe7Xt3SekAJwmcDaVYANRLbKeARJFIuYDeu/cxT5vOXzdusvT5eABuXu/EPG06PT29SgLUYCCyehJ8N4oNI56IQqX0EeCX3JX8mhEFKJXSUSkoeV3VJz5TqgqQ1uxHFVTo/pUP6y/ytn0BxekLZX61MyEQKKXiDHkU7PP+zuEjlexruKToVxQg7b2aLRrkOxLJzMwk37FA0S+bAqUej2UUdq5MZJf7LO+tSFT0jzgF/xVGWpBj/jdUg+Lf0/8oYFQLZlS/1wqXMBal/wJknuGtsItVcQAAAABJRU5ErkJggg==
""" # Replace this with your actual base64 icon
def get_icon_from_base64(icon_base64):
image_data = base64.b64decode(icon_base64)
image = Image.open(BytesIO(image_data))
bytes_io = BytesIO()
image.save(bytes_io, format='PNG')
qpixmap = QPixmap()
qpixmap.loadFromData(bytes_io.getvalue())
return QIcon(qpixmap)
def calculate_checksum(file_path, algorithm='md5'):
hash_func = getattr(hashlib, algorithm)()
with open(file_path, 'rb') as f:
for chunk in iter(lambda: f.read(4096), b''):
hash_func.update(chunk)
return hash_func.hexdigest()
class ChecksumApp(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
self.setWindowTitle("MD5 & SHA Checksum Utility v1.0")
self.setWindowIcon(get_icon_from_base64(icon_base64))
self.setGeometry(400, 100, 500, 300)
grid = QGridLayout()
# File label, entry, and browse button
file_label = QLabel("File:")
grid.addWidget(file_label, 0, 0)
self.file_entry = QLineEdit()
grid.addWidget(self.file_entry, 0, 1)
self.browse_button = QPushButton("Browse")
self.browse_button.clicked.connect(self.open_file)
grid.addWidget(self.browse_button, 0, 2)
# Checkboxes and hash result fields
self.md5_var = QCheckBox("MD5")
self.md5_var.setChecked(True)
grid.addWidget(self.md5_var, 1, 0)
self.md5_result = QLineEdit()
grid.addWidget(self.md5_result, 1, 1)
self.md5_copy_button = QPushButton("Copy MD5")
self.md5_copy_button.clicked.connect(lambda: self.copy_to_clipboard(self.md5_result.text()))
grid.addWidget(self.md5_copy_button, 1, 2)
self.sha1_var = QCheckBox("SHA-1")
self.sha1_var.setChecked(True)
grid.addWidget(self.sha1_var, 2, 0)
self.sha1_result = QLineEdit()
grid.addWidget(self.sha1_result, 2, 1)
self.sha1_copy_button = QPushButton("Copy SHA-1")
self.sha1_copy_button.clicked.connect(lambda: self.copy_to_clipboard(self.sha1_result.text()))
grid.addWidget(self.sha1_copy_button, 2, 2)
self.sha256_var = QCheckBox("SHA-256")
self.sha256_var.setChecked(True)
grid.addWidget(self.sha256_var, 3, 0)
self.sha256_result = QLineEdit()
grid.addWidget(self.sha256_result, 3, 1)
self.sha256_copy_button = QPushButton("Copy SHA-256")
self.sha256_copy_button.clicked.connect(lambda: self.copy_to_clipboard(self.sha256_result.text()))
grid.addWidget(self.sha256_copy_button, 3, 2)
self.sha512_var = QCheckBox("SHA-512")
self.sha512_var.setChecked(True)
grid.addWidget(self.sha512_var, 4, 0)
self.sha512_result = QLineEdit()
grid.addWidget(self.sha512_result, 4, 1)
self.sha512_copy_button = QPushButton("Copy SHA-512")
self.sha512_copy_button.clicked.connect(lambda: self.copy_to_clipboard(self.sha512_result.text()))
grid.addWidget(self.sha512_copy_button, 4, 2)
# Save results button
self.save_button = QPushButton("Save Results")
self.save_button.clicked.connect(self.save_results)
grid.addWidget(self.save_button, 5, 1)
# Verify Hash section
verify_label = QLabel("Verify Hash with Generated Hash (MD5, SHA-1, SHA-256, or SHA-512)")
grid.addWidget(verify_label, 6, 0, 1, 3)
self.hash_entry = QLineEdit()
grid.addWidget(self.hash_entry, 7, 1)
self.verify_button = QPushButton("Verify")
self.verify_button.clicked.connect(self.verify_hash)
grid.addWidget(self.verify_button, 7, 2)
# Set layout
self.setLayout(grid)
# Enable drag and drop
self.setAcceptDrops(True)
# Function to open file dialog and calculate checksums
def open_file(self):
file_path, _ = QFileDialog.getOpenFileName(self, 'Open File')
if file_path:
self.file_entry.setText(file_path)
self.display_checksums(file_path)
# Function to display checksums
def display_checksums(self, file_path):
try:
if self.md5_var.isChecked():
md5_checksum = calculate_checksum(file_path, 'md5')
self.md5_result.setText(md5_checksum)
if self.sha1_var.isChecked():
sha1_checksum = calculate_checksum(file_path, 'sha1')
self.sha1_result.setText(sha1_checksum)
if self.sha256_var.isChecked():
sha256_checksum = calculate_checksum(file_path, 'sha256')
self.sha256_result.setText(sha256_checksum)
if self.sha512_var.isChecked():
sha512_checksum = calculate_checksum(file_path, 'sha512')
self.sha512_result.setText(sha512_checksum)
except Exception as e:
QMessageBox.critical(self, "Error", f"An error occurred: {e}")
# Function to copy checksum to clipboard
def copy_to_clipboard(self, checksum):
clipboard = QApplication.clipboard()
clipboard.setText(checksum)
QMessageBox.information(self, "Copied", "Checksum copied to clipboard!")
# Function to save results to a file
def save_results(self):
result_text = f"File: {self.file_entry.text()}\n\n"
if self.md5_var.isChecked():
result_text += f"MD5: {self.md5_result.text()}\n"
if self.sha1_var.isChecked():
result_text += f"SHA1: {self.sha1_result.text()}\n"
if self.sha256_var.isChecked():
result_text += f"SHA256: {self.sha256_result.text()}\n"
if self.sha512_var.isChecked():
result_text += f"SHA512: {self.sha512_result.text()}\n"
file_path, _ = QFileDialog.getSaveFileName(self, 'Save File', '', 'Text files (*.txt)')
if file_path:
with open(file_path, 'w') as f:
f.write(result_text)
QMessageBox.information(self, "Saved", "Results saved to file!")
# Function to verify the hash
def verify_hash(self):
input_hash = self.hash_entry.text().strip()
if (self.md5_result.text() == input_hash or
self.sha1_result.text() == input_hash or
self.sha256_result.text() == input_hash or
self.sha512_result.text() == input_hash):
QMessageBox.information(self, "Success", "The input hash matches the calculated hash.")
else:
QMessageBox.critical(self, "Mismatch", "The input hash does not match any calculated hash.")
# Enable drag and drop functionality
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.acceptProposedAction()
def dropEvent(self, event):
urls = event.mimeData().urls()
if urls:
file_path = urls[0].toLocalFile()
self.file_entry.setText(file_path)
self.display_checksums(file_path)
# Run the application
if __name__ == '__main__':
app = QApplication(sys.argv)
window = ChecksumApp()
window.show()
sys.exit(app.exec_())