2019-01-13 16:30:07 +00:00
|
|
|
#include <crypto/types.hpp>
|
|
|
|
|
|
|
|
#include <util/buffer.hpp>
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <iterator>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
bool
|
|
|
|
PubKey::FromString(const std::string& str)
|
|
|
|
{
|
|
|
|
return HexDecode(str.c_str(), begin(), size());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
PubKey::ToString() const
|
|
|
|
{
|
|
|
|
char buf[(PUBKEYSIZE + 1) * 2] = {0};
|
|
|
|
return HexEncode(*this, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SecretKey::LoadFromFile(const char* fname)
|
|
|
|
{
|
2019-01-15 23:07:02 +00:00
|
|
|
std::ifstream f(fname, std::ios::in | std::ios::binary);
|
|
|
|
|
2019-01-13 16:30:07 +00:00
|
|
|
if(!f.is_open())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-15 00:42:50 +00:00
|
|
|
|
2019-01-13 16:30:07 +00:00
|
|
|
f.seekg(0, std::ios::end);
|
2019-01-15 00:42:50 +00:00
|
|
|
const size_t sz = f.tellg();
|
2019-01-13 16:30:07 +00:00
|
|
|
f.seekg(0, std::ios::beg);
|
2019-01-15 00:42:50 +00:00
|
|
|
|
2019-01-13 16:30:07 +00:00
|
|
|
if(sz == size())
|
|
|
|
{
|
|
|
|
// is raw buffer
|
2019-01-15 23:07:02 +00:00
|
|
|
std::copy_n(std::istreambuf_iterator< char >(f), sz, begin());
|
2019-01-13 16:30:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-01-15 00:42:50 +00:00
|
|
|
std::array< byte_t, 128 > tmp;
|
2019-02-02 23:12:42 +00:00
|
|
|
llarp_buffer_t buf(tmp);
|
2019-01-13 16:30:07 +00:00
|
|
|
if(sz > sizeof(tmp))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-15 00:42:50 +00:00
|
|
|
f.read(reinterpret_cast< char* >(tmp.data()), sz);
|
2019-01-13 16:30:07 +00:00
|
|
|
return BDecode(&buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SecretKey::SaveToFile(const char* fname) const
|
|
|
|
{
|
2019-02-02 23:12:42 +00:00
|
|
|
std::array< byte_t, 128 > tmp;
|
|
|
|
llarp_buffer_t buf(tmp);
|
2019-01-13 16:30:07 +00:00
|
|
|
if(!BEncode(&buf))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ofstream f;
|
|
|
|
f.open(fname, std::ios::binary);
|
|
|
|
if(!f.is_open())
|
|
|
|
return false;
|
|
|
|
f.write((char*)buf.base, buf.cur - buf.base);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-21 15:45:18 +00:00
|
|
|
bool
|
|
|
|
IdentitySecret::LoadFromFile(const char* fname)
|
|
|
|
{
|
|
|
|
std::ifstream f(fname, std::ios::binary | std::ios::in);
|
|
|
|
if(!f.is_open())
|
|
|
|
return false;
|
|
|
|
f.seekg(0, std::ios::end);
|
|
|
|
const size_t sz = f.tellg();
|
|
|
|
f.seekg(0, std::ios::beg);
|
|
|
|
if(sz != 32)
|
|
|
|
{
|
|
|
|
llarp::LogError("service node seed size invalid: ", sz, " != 32");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::copy_n(std::istreambuf_iterator< char >(f), sz, begin());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-13 16:30:07 +00:00
|
|
|
} // namespace llarp
|