-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
152 lines (109 loc) · 3.3 KB
/
main.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
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
#include "Matrix.hpp"
#include "DataSplitter.hpp"
#include "Buffer.hpp"
#include "DataSeparator.hpp"
#include "ClientDataPart.hpp"
#include "Base16.hpp"
vector<ClientDataPart> separate(const Buffer &secret, const uint16_t K, const uint16_t N){
DataSeparator dataSeparator(K);
vector<vector<uint32_t>> separatedSecret = dataSeparator.separate(secret);
DataSplitter dataSplitter;
vector<Matrix> splittedSecret;
for(const auto &secretPart : separatedSecret){
Matrix equasions = dataSplitter.split(secretPart, K, N);
splittedSecret.push_back(move(equasions));
}
vector<ClientDataPart> clients;
for(uint32_t client = 0; client < N; ++client){
Matrix currentClientMatrix;
for(const auto &equasion : splittedSecret)
currentClientMatrix.pushRowBack(equasion.getRow(client));
ClientDataPart currentCDP(currentClientMatrix);
clients.push_back(move(currentCDP));
}
return clients;
}
Buffer restore(const vector<ClientDataPart> &clients){
if(clients.size() == 0)
throw runtime_error("empty input");
uint16_t K = clients.size();
uint32_t partCount = clients.front().getLinearEquasions().getRowCount();
vector<Matrix> splittedSecret(partCount);
for(uint32_t part = 0; part < partCount; ++part)
for(uint32_t client = 0; client < K; ++client)
splittedSecret.at(part).pushRowBack(clients.at(client).getLinearEquasions().getRow(part));
DataSplitter dataSplitter;
vector<vector<uint32_t>> restored;
for(const auto &matrix : splittedSecret){
vector<uint32_t> secretPart = move(dataSplitter.restore(matrix, K));
restored.push_back(secretPart);
}
DataSeparator dataSeparator(K);
Buffer result = move(dataSeparator.restore(restored));
return result;
}
void man(const string &progName){
cout << "Usage: " << endl;
cout << progName << " <-separate N K secret>|<-restore part1 part2 ... partK>" << endl;
}
int main(int argc, char **argv){//./progName <-separate N K key>|<-restore part1 part2 ...>
const string progName(argv[0]);
Base16 base16;
if(argc < 2){
man(progName);
return -1;
}
const string action(argv[1]);
if(action == "-separate"){
if(argc < 5){
man(progName);
return -1;
}
const string Nstr(argv[2]);
const string Kstr(argv[3]);
uint16_t N = stoi(Nstr);
uint16_t K = stoi(Kstr);
const string key(argv[4]);
vector<ClientDataPart> parts;
try{
parts = move(separate(key, K, N));
}catch(runtime_error &re){
cout << "Ошибка: " << re.what() << endl;
}catch(logic_error &le){
cout << "Ошибка: " << le.what() << endl;
}
size_t i = 0;
for(const auto &part : parts){
Buffer bc = move(part.toBinaryContainer());
string bc16 = base16.encode(bc);
cout << ++i << " : " << bc16 << endl << endl;
}
}
else if(action == "-restore"){
if(argc < 3){
man(progName);
return -1;
}
vector<ClientDataPart> parts;
for(uint16_t i = 2; i < argc; ++i){
string bc16(argv[i]);
Buffer bc = base16.decode(bc16);
ClientDataPart cdp(bc);
parts.push_back(move(cdp));
}
Buffer result;
try{
result = move(restore(parts));
}catch(runtime_error &re){
cout << "Ошибка: " << re.what() << endl;
}catch(logic_error &le){
cout << "Ошибка: " << le.what() << endl;
}
cout << result.toCharString() << endl;
}
}