2018-12-12 00:38:58 +00:00
|
|
|
#include <crypto.hpp>
|
2018-12-13 16:14:44 +00:00
|
|
|
#include <buffer.hpp>
|
2018-12-10 17:22:59 +00:00
|
|
|
|
2019-01-03 21:25:09 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <iterator>
|
|
|
|
|
2018-12-10 17:22:59 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
bool
|
|
|
|
PubKey::FromString(const std::string& str)
|
|
|
|
{
|
2019-01-02 01:04:04 +00:00
|
|
|
return HexDecode(str.c_str(), begin(), size());
|
2018-12-10 17:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2019-01-02 01:03:53 +00:00
|
|
|
std::copy(std::istream_iterator< byte_t >(f),
|
|
|
|
std::istream_iterator< byte_t >(), begin());
|
2018-12-13 16:14:44 +00:00
|
|
|
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
|