1
0
mirror of https://github.com/PurpleI2P/i2pd-tools synced 2025-01-15 17:20:08 +00:00
i2pd-tools/regaddralias.cpp

96 lines
2.4 KiB
C++
Raw Normal View History

2020-11-14 21:39:53 -05:00
#include <iostream>
#include <fstream>
#include <sstream>
#include "Identity.h"
#include "Base.h"
int main (int argc, char * argv[])
{
if (argc < 3)
{
std::cout << "Usage: regaddralias oldfilename newfilename address" << std::endl;
return -1;
}
2024-05-07 18:13:03 -04:00
i2p::crypto::InitCrypto (false, true, false);
2020-11-14 21:39:53 -05:00
i2p::data::PrivateKeys oldkeys, newkeys;
{
std::ifstream s(argv[1], std::ifstream::binary);
if (s.is_open ())
{
2020-11-14 21:39:53 -05:00
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(!oldkeys.FromBuffer (buf, len))
{
std::cout << "Failed to load keyfile " << argv[1] << std::endl;
2020-11-14 21:39:53 -05:00
return -1;
}
delete[] buf;
}
2020-11-14 21:39:53 -05:00
else
{
std::cout << "Can't open keyfile " << argv[1] << std::endl;
return -1;
2020-11-14 21:39:53 -05:00
}
}
{
std::ifstream s(argv[2], std::ifstream::binary);
if (s.is_open ())
{
2020-11-14 21:39:53 -05:00
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(!newkeys.FromBuffer (buf, len))
{
std::cout << "Failed to load keyfile " << argv[2] << std::endl;
2020-11-14 21:39:53 -05:00
return -1;
}
delete[] buf;
}
2020-11-14 21:39:53 -05:00
else
{
std::cout << "Can't open keyfile " << argv[2] << std::endl;
2020-11-14 21:39:53 -05:00
return -1;
}
}
std::stringstream out;
out << argv[3] << "="; // address
out << newkeys.GetPublic ()->ToBase64 ();
out << "#!action=adddest#olddest=";
out << oldkeys.GetPublic ()->ToBase64 ();
auto oldSignatureLen = oldkeys.GetPublic ()->GetSignatureLen ();
uint8_t * oldSignature = new uint8_t[oldSignatureLen];
char * oldSig = new char[oldSignatureLen*2];
2020-11-14 21:39:53 -05:00
oldkeys.Sign ((uint8_t *)out.str ().c_str (), out.str ().length (), oldSignature);
auto len = i2p::data::ByteStreamToBase64 (oldSignature, oldSignatureLen, oldSig, oldSignatureLen*2);
oldSig[len] = 0;
out << "#oldsig=" << oldSig;
delete[] oldSignature;
delete[] oldSig;
auto signatureLen = newkeys.GetPublic ()->GetSignatureLen ();
uint8_t * signature = new uint8_t[signatureLen];
char * sig = new char[signatureLen*2];
2020-11-14 21:39:53 -05:00
newkeys.Sign ((uint8_t *)out.str ().c_str (), out.str ().length (), signature);
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;
2020-11-14 21:39:53 -05:00
i2p::crypto::TerminateCrypto ();
return 0;
}