-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsha.cpp
More file actions
196 lines (170 loc) · 6.15 KB
/
sha.cpp
File metadata and controls
196 lines (170 loc) · 6.15 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
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
#include <iostream>
#include <iomanip>
#include <openssl/evp.h>
#include <fstream>
#include <memory>
void computeFileSHA256(const std::string& filePath) {
std::ifstream file(filePath, std::ios::binary);
if (!file) {
std::cerr << "Unable to open file: " << filePath << std::endl;
return;
}
EVP_MD_CTX* mdctx = EVP_MD_CTX_new();
if (!mdctx) {
std::cerr << "Failed to create digest context." << std::endl;
return;
}
if (EVP_DigestInit_ex(mdctx, EVP_sha256(), nullptr) != 1) {
std::cerr << "Failed to initialize digest context." << std::endl;
EVP_MD_CTX_free(mdctx);
return;
}
const size_t bufferSize = 32 * 1024 * 1024;
std::unique_ptr<char[]> buffer(new char[bufferSize]);
while (file.read(buffer.get(), bufferSize) || file.gcount() > 0) {
if (EVP_DigestUpdate(mdctx, buffer.get(), file.gcount()) != 1) {
std::cerr << "Failed to update hash." << std::endl;
EVP_MD_CTX_free(mdctx);
return;
}
}
unsigned char hash[EVP_MAX_MD_SIZE];
unsigned int hashLength = 0;
if (EVP_DigestFinal_ex(mdctx, hash, &hashLength) != 1) {
std::cerr << "Failed to finalize hash." << std::endl;
EVP_MD_CTX_free(mdctx);
return;
}
EVP_MD_CTX_free(mdctx);
std::cout << "SHA-256 hash: ";
for (unsigned int i = 0; i < hashLength; i++) {
std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(hash[i]);
}
std::cout << std::endl;
}
void computeFileSHA384(const std::string& filePath) {
std::ifstream file(filePath, std::ios::binary);
if (!file) {
std::cerr << "Unable to open file: " << filePath << std::endl;
return;
}
EVP_MD_CTX* mdctx = EVP_MD_CTX_new();
if (!mdctx) {
std::cerr << "Failed to create digest context." << std::endl;
return;
}
// Use SHA-384 algorithm
if (EVP_DigestInit_ex(mdctx, EVP_sha384(), nullptr) != 1) {
std::cerr << "Failed to initialize digest context with SHA-384." << std::endl;
EVP_MD_CTX_free(mdctx);
return;
}
const size_t bufferSize = 32 * 1024 * 1024;
std::unique_ptr<char[]> buffer(new char[bufferSize]);
while (file.read(buffer.get(), bufferSize) || file.gcount() > 0) {
if (EVP_DigestUpdate(mdctx, buffer.get(), file.gcount()) != 1) {
std::cerr << "Failed to update hash." << std::endl;
EVP_MD_CTX_free(mdctx);
return;
}
}
unsigned char hash[EVP_MAX_MD_SIZE];
unsigned int hashLength = 0;
if (EVP_DigestFinal_ex(mdctx, hash, &hashLength) != 1) {
std::cerr << "Failed to finalize hash." << std::endl;
EVP_MD_CTX_free(mdctx);
return;
}
EVP_MD_CTX_free(mdctx);
// Output SHA-384 hash
std::cout << "SHA-384 hash: ";
for (unsigned int i = 0; i < hashLength; i++) {
std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(hash[i]);
}
std::cout << std::endl;
}
void computeFileSHA512(const std::string& filePath) {
std::ifstream file(filePath, std::ios::binary);
if (!file) {
std::cerr << "Unable to open file: " << filePath << std::endl;
return;
}
EVP_MD_CTX* mdctx = EVP_MD_CTX_new();
if (!mdctx) {
std::cerr << "Failed to create digest context." << std::endl;
return;
}
// Use SHA-512 algorithm
if (EVP_DigestInit_ex(mdctx, EVP_sha512(), nullptr) != 1) {
std::cerr << "Failed to initialize digest context with SHA-384." << std::endl;
EVP_MD_CTX_free(mdctx);
return;
}
const size_t bufferSize = 32 * 1024 * 1024;
std::unique_ptr<char[]> buffer(new char[bufferSize]);
while (file.read(buffer.get(), bufferSize) || file.gcount() > 0) {
if (EVP_DigestUpdate(mdctx, buffer.get(), file.gcount()) != 1) {
std::cerr << "Failed to update hash." << std::endl;
EVP_MD_CTX_free(mdctx);
return;
}
}
unsigned char hash[EVP_MAX_MD_SIZE];
unsigned int hashLength = 0;
if (EVP_DigestFinal_ex(mdctx, hash, &hashLength) != 1) {
std::cerr << "Failed to finalize hash." << std::endl;
EVP_MD_CTX_free(mdctx);
return;
}
EVP_MD_CTX_free(mdctx);
// Output SHA-512 hash
std::cout << "SHA-512 hash: ";
for (unsigned int i = 0; i < hashLength; i++) {
std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(hash[i]);
}
std::cout << std::endl;
}
void computeFileKeccak256(const std::string& filePath) {
std::ifstream file(filePath, std::ios::binary);
if (!file) {
std::cerr << "Unable to open file: " << filePath << std::endl;
return;
}
// Tworzenie kontekstu dla hashowania
EVP_MD_CTX* mdctx = EVP_MD_CTX_new();
if (!mdctx) {
std::cerr << "Failed to create digest context." << std::endl;
return;
}
// Inicjalizacja kontekstu z algorytmem Keccak256 (SHA3-256)
if (EVP_DigestInit_ex(mdctx, EVP_sha3_256(), nullptr) != 1) {
std::cerr << "Failed to initialize digest context." << std::endl;
EVP_MD_CTX_free(mdctx);
return;
}
const size_t bufferSize = 32 * 1024 * 1024;
std::unique_ptr<char[]> buffer(new char[bufferSize]);
// Aktualizacja hashu danymi z pliku
while (file.read(buffer.get(), bufferSize) || file.gcount() > 0) {
if (EVP_DigestUpdate(mdctx, buffer.get(), file.gcount()) != 1) {
std::cerr << "Failed to update hash." << std::endl;
EVP_MD_CTX_free(mdctx);
return;
}
}
// Finalizacja hashowania
unsigned char hash[EVP_MAX_MD_SIZE];
unsigned int hashLength = 0;
if (EVP_DigestFinal_ex(mdctx, hash, &hashLength) != 1) {
std::cerr << "Failed to finalize hash." << std::endl;
EVP_MD_CTX_free(mdctx);
return;
}
EVP_MD_CTX_free(mdctx);
// Wyświetlanie wyniku w formacie szesnastkowym
std::cout << "Keccak256 (SHA3-256) hash: ";
for (unsigned int i = 0; i < hashLength; i++) {
std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(hash[i]);
}
std::cout << std::endl;
}