-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhasher.cpp
181 lines (152 loc) · 5.98 KB
/
hasher.cpp
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
#include <openssl/evp.h>
#include <openssl/err.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <fstream>
#include <wx/wx.h>
#include <wx/filedlg.h>
#include <wx/clipbrd.h>
class FileFn {
public:
FileFn(const std::string& filename, std::string& hashing) : filename(filename), hashing(hashing){
std::ifstream file(filename, std::ios::binary);
if (!file) {
throw std::runtime_error("Could not open file");
}
EVP_MD_CTX* mdctx = EVP_MD_CTX_new();
if (mdctx == nullptr) {
throw std::runtime_error("Failed to create EVP_MD_CTX");
}
if (hashing == "SHA-512") {
if (EVP_DigestInit_ex(mdctx, EVP_sha512(), nullptr) != 1) {
EVP_MD_CTX_free(mdctx);
throw std::runtime_error("Failed to initialize digest");
}
} else if (hashing == "SHA-256") {
if (EVP_DigestInit_ex(mdctx, EVP_sha256(), nullptr) != 1) {
EVP_MD_CTX_free(mdctx);
throw std::runtime_error("Failed to initialize digest");
}
} else {
throw std::runtime_error("Unsupported hashing algorithm");
}
char buffer[4096];
while (file.good()) {
file.read(buffer, sizeof(buffer));
if (EVP_DigestUpdate(mdctx, buffer, file.gcount()) != 1) {
EVP_MD_CTX_free(mdctx);
throw std::runtime_error("Failed to update digest");
}
}
unsigned char md_value[EVP_MAX_MD_SIZE];
unsigned int md_len;
if (EVP_DigestFinal_ex(mdctx, md_value, &md_len) != 1) {
EVP_MD_CTX_free(mdctx);
throw std::runtime_error("Failed to finalize digest");
}
EVP_MD_CTX_free(mdctx);
std::ostringstream oss;
for (unsigned int i = 0; i < md_len; ++i) {
oss << std::hex << std::setw(2) << std::setfill('0') << (int)md_value[i];
}
this->hash = oss.str();
}
std::string hashing;
std::string hash;
private:
std::string filename;
};
class MyApp : public wxApp {
public:
virtual bool OnInit();
};
class MyFrame : public wxFrame {
public:
MyFrame(const wxString& title);
private:
void OnOpenFile(wxCommandEvent& event);
void OnCalculateHash(wxCommandEvent& event);
void OnCopyHash(wxCommandEvent& event);
void OnExit(wxCommandEvent& event);
wxTextCtrl* filenameEntry;
wxTextCtrl* hashEntry;
wxChoice* hashChoice;
};
wxIMPLEMENT_APP(MyApp);
bool MyApp::OnInit() {
MyFrame* frame = new MyFrame("File Hash Calculator");
frame->Show(true);
return true;
}
MyFrame::MyFrame(const wxString& title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(400, 190)) {
wxPanel* panel = new wxPanel(this, wxID_ANY);
wxMenuBar* menuBar = new wxMenuBar;
wxMenu* fileMenu = new wxMenu;
fileMenu->Append(wxID_OPEN, "&Open\tCtrl-O", "Open a file");
fileMenu->AppendSeparator();
fileMenu->Append(wxID_EXIT, "E&xit\tCtrl-Q", "Exit the application");
menuBar->Append(fileMenu, "&File");
SetMenuBar(menuBar);
wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL);
wxBoxSizer* fileBox = new wxBoxSizer(wxHORIZONTAL);
wxButton* openButton = new wxButton(panel, wxID_ANY, "Open File");
filenameEntry = new wxTextCtrl(panel, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxTE_READONLY);
fileBox->Add(openButton, 0, wxRIGHT, 5);
fileBox->Add(filenameEntry, 1);
hashEntry = new wxTextCtrl(panel, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxTE_READONLY);
wxButton* copyButton = new wxButton(panel, wxID_ANY, "Copy Hash");
wxButton* calculateButton = new wxButton(panel, wxID_ANY, "Calculate Hash");
hashChoice = new wxChoice(panel, wxID_ANY);
hashChoice->Append("SHA-256");
hashChoice->Append("SHA-512");
hashChoice->SetSelection(0);
vbox->Add(fileBox, 0, wxEXPAND | wxALL, 5);
vbox->Add(hashEntry, 0, wxEXPAND | wxALL, 5);
vbox->Add(hashChoice, 0, wxEXPAND | wxALL, 5);
wxBoxSizer* buttonBox = new wxBoxSizer(wxHORIZONTAL);
buttonBox->Add(calculateButton, 1, wxRIGHT, 5);
buttonBox->Add(copyButton, 1);
vbox->Add(buttonBox, 0, wxEXPAND | wxALL, 5);
wxStaticText* copyrightLabel = new wxStaticText(panel, wxID_ANY, "© by A. Slatina");
vbox->Add(copyrightLabel, 0, wxALIGN_CENTER | wxALL, 5);
panel->SetSizer(vbox);
Bind(wxEVT_BUTTON, &MyFrame::OnOpenFile, this, openButton->GetId());
Bind(wxEVT_BUTTON, &MyFrame::OnCalculateHash, this, calculateButton->GetId());
Bind(wxEVT_BUTTON, &MyFrame::OnCopyHash, this, copyButton->GetId());
Bind(wxEVT_MENU, &MyFrame::OnOpenFile, this, wxID_OPEN);
Bind(wxEVT_MENU, &MyFrame::OnExit, this, wxID_EXIT);
}
void MyFrame::OnOpenFile(wxCommandEvent& event) {
wxFileDialog openFileDialog(this, _("Open File"), "", "", "All files (*.*)|*.*", wxFD_OPEN | wxFD_FILE_MUST_EXIST);
if (openFileDialog.ShowModal() == wxID_CANCEL) {
return;
}
filenameEntry->SetValue(openFileDialog.GetPath());
}
void MyFrame::OnCalculateHash(wxCommandEvent& event) {
wxString filename = filenameEntry->GetValue();
if (!filename.IsEmpty()) {
try {
std::string hash_type = hashChoice->GetStringSelection().ToStdString();
FileFn file(filename.ToStdString(), hash_type);
hashEntry->SetValue(file.hash);
int hash_length = file.hash.length();
int new_width = hash_length * 8;
SetSize(wxSize(new_width, 190));
} catch (const std::exception& e) {
wxMessageBox("Error processing file: " + wxString(e.what()), "Error", wxOK | wxICON_ERROR);
}
}
}
void MyFrame::OnCopyHash(wxCommandEvent& event) {
if (wxTheClipboard->Open()) {
wxTheClipboard->SetData(new wxTextDataObject(hashEntry->GetValue()));
wxTheClipboard->Close();
wxMessageBox("Hash copied to clipboard", "Info", wxOK | wxICON_INFORMATION);
}
}
void MyFrame::OnExit(wxCommandEvent& event) {
Close(true);
}