2018-12-12 00:38:58 +00:00
|
|
|
#include <crypto.hpp>
|
2018-12-13 16:14:44 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <buffer.hpp>
|
2018-12-10 17:22:59 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
bool
|
|
|
|
PubKey::FromString(const std::string& str)
|
|
|
|
{
|
|
|
|
return HexDecode(str.c_str(), data(), size());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
PubKey::ToString() const
|
|
|
|
{
|
|
|
|
char buf[(PUBKEYSIZE + 1) * 2] = {0};
|
|
|
|
return HexEncode(*this, buf);
|
|
|
|
}
|
2018-12-13 16:14:44 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
SecretKey::LoadFromFile(const char* fname)
|
|
|
|
{
|
|
|
|
std::ifstream f;
|
|
|
|
f.open(fname, std::ios::binary);
|
|
|
|
if(!f.is_open())
|
|
|
|
return false;
|
|
|
|
size_t sz = 0;
|
|
|
|
f.seekg(0, std::ios::end);
|
|
|
|
sz = f.tellg();
|
|
|
|
f.seekg(0, std::ios::beg);
|
|
|
|
if(sz == size())
|
|
|
|
{
|
|
|
|
// is raw buffer
|
|
|
|
f.read((char*)data(), 64);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
byte_t tmp[128];
|
|
|
|
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
|
|
|
if(sz > sizeof(tmp))
|
|
|
|
return false;
|
|
|
|
f.read((char*)tmp, sz);
|
|
|
|
return BDecode(&buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SecretKey::SaveToFile(const char* fname) const
|
|
|
|
{
|
|
|
|
byte_t tmp[128];
|
|
|
|
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-12-10 17:22:59 +00:00
|
|
|
} // namespace llarp
|