lokinet/llarp/crypto.cpp

62 lines
1.2 KiB
C++
Raw Normal View History

2018-12-12 00:38:58 +00:00
#include <crypto.hpp>
#include <fstream>
#include <buffer.hpp>
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);
}
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;
}
} // namespace llarp