-
Notifications
You must be signed in to change notification settings - Fork 37
/
regaddr_3ld.cpp
130 lines (121 loc) · 3.53 KB
/
regaddr_3ld.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
#include <iostream>
#include <fstream>
#include <sstream>
#include <ctime>
#include "Identity.h"
#include "Base.h"
static void help ()
{
std::cout << "Usage:" << std::endl;
std::cout << "\treg3ldaddr step1 privkey address" << std::endl;
std::cout << "\treg3ldaddr step2 step1file oldprivkey oldaddress" << std::endl;
std::cout << "\treg3ldaddr step3 step2file privkey" << std::endl;
}
int main (int argc, char * argv[])
{
if (argc < 3) { help(); return -1;}
std::string arg = argv[1];
i2p::crypto::InitCrypto (false, true, false);
i2p::data::PrivateKeys keys;
if (arg == "step1") {
std::ifstream s(argv[2], std::ifstream::binary);
if (s.is_open ()) {
s.seekg (0, std::ios::end);
size_t len = s.tellg();
s.seekg (0, std::ios::beg);
uint8_t * buf = new uint8_t[len];
s.read ((char *)buf, len);
if(keys.FromBuffer (buf, len)) {
std::stringstream out;
out << argv[3] << "="; // address
out << keys.GetPublic ()->ToBase64 ();
out << "#!action=addsubdomain";
std::cout << out.str () << std::endl;
} else
std::cout << "Failed to load keyfile " << argv[1] << std::endl;
delete[] buf;
}
}
else if (arg == "step2") {
std::ifstream t(argv[2]);
std::ifstream s(argv[3], std::ifstream::binary);
std::string regtxt;
std::stringstream out;
if (t.is_open ()) {
while (t.good()) {
getline (t, regtxt);
out << regtxt;
}
t.close();
} else {
std::cout << "Failed to read file with STEP1 output";
exit(1);
}
if (s.is_open ()) {
s.seekg (0, std::ios::end);
size_t len = s.tellg();
s.seekg (0, std::ios::beg);
uint8_t * buf = new uint8_t[len];
s.read ((char *)buf, len);
if(keys.FromBuffer (buf, len)) {
auto signatureLen = keys.GetPublic ()->GetSignatureLen ();
uint8_t * signature = new uint8_t[signatureLen];
char * sig = new char[signatureLen*2];
out << "#date=" << std::time(nullptr);
out << "#olddest=" << keys.GetPublic ()->ToBase64 ();
out << "#oldname=" << argv[4];
keys.Sign ((uint8_t *)out.str ().c_str (), out.str ().length (), signature);
auto len = i2p::data::ByteStreamToBase64 (signature, signatureLen, sig, signatureLen*2);
sig[len] = 0;
out << "#oldsig=" << sig;
delete[] signature;
delete[] sig;
std::cout << out.str () << std::endl;
} else
std::cout << "Failed to load keyfile " << argv[1] << std::endl;
delete[] buf;
}
}
else if (arg == "step3") {
std::ifstream t(argv[2]);
std::ifstream s(argv[3], std::ifstream::binary);
std::string regtxt;
std::stringstream out;
if (t.is_open ()) {
while (t.good()) {
getline (t, regtxt);
out << regtxt;
}
t.close();
} else {
std::cout << "Failed to read file with STEP2 output";
exit(1);
}
if (s.is_open ()) {
s.seekg (0, std::ios::end);
size_t len = s.tellg();
s.seekg (0, std::ios::beg);
uint8_t * buf = new uint8_t[len];
s.read ((char *)buf, len);
if(keys.FromBuffer (buf, len)) {
auto signatureLen = keys.GetPublic ()->GetSignatureLen ();
uint8_t * signature = new uint8_t[signatureLen];
char * sig = new char[signatureLen*2];
keys.Sign ((uint8_t *)out.str ().c_str (), out.str ().length (), signature);
auto len = i2p::data::ByteStreamToBase64 (signature, signatureLen, sig, signatureLen*2);
sig[len] = 0;
out << "#sig=" << sig;
delete[] signature;
delete[] sig;
std::cout << out.str () << std::endl;
} else
std::cout << "Failed to load keyfile " << argv[1] << std::endl;
delete[] buf;
}
}
else {
help(); exit(1);
}
i2p::crypto::TerminateCrypto ();
return 0;
}