-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd5_sum.cpp
80 lines (54 loc) · 1.73 KB
/
md5_sum.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
#include <iostream>
#include <openssl/md5.h>
#include <sstream>
#include <iomanip>
#include <boost/asio.hpp>
#include <fstream>
#include <openssl/md5.h>
#include <fstream>
std::string get_file_size(const std::string& file_name) {
std::ifstream file(file_name, std::ios::binary | std::ios::ate);
if (!file) {
std::cerr << "Failed to open the file.\n";
return {};
}
std::streamsize size = file.tellg();
file.close();
return std::to_string(size);
}
std::string get_md5_hash(const std::string& file_name) {
std::ifstream file(file_name, std::ios::binary);
if (!file) {
std::cerr << "Failed to open the file.\n";
return {};
}
MD5_CTX md5Context;
MD5_Init(&md5Context);
char buf[1024 * 16];
while (file.good()) {
file.read(buf, sizeof(buf));
MD5_Update(&md5Context, buf, file.gcount());
}
unsigned char result[MD5_DIGEST_LENGTH];
MD5_Final(result, &md5Context);
std::ostringstream sout;
sout << std::hex << std::setfill('0');
for (auto c : result) {
sout << std::setw(2) << static_cast<int>(c);
}
return sout.str();
}
void start_sending(boost::asio::ip::tcp::socket& socket, const std::string& file_name) {
std::string md5_hash = get_md5_hash(file_name);
std::string file_size = get_file_size(file_name);
std::string hash_and_size = md5_hash + "|" + file_size + "\n";
boost::asio::write(socket, boost::asio::buffer(hash_and_size));
}
int main() {
boost::asio::io_context io_context;
boost::asio::ip::tcp::socket socket(io_context);
socket.connect(endpoint);
std::string file_name = "文件名";
start_sending(socket, file_name);
return 0;
}