You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lokinet/llarp/crypto/types.cpp

131 lines
2.6 KiB
C++

#include <crypto/types.hpp>
#include <util/buffer.hpp>
#include <fstream>
#include <util/fs.hpp>
#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)
{
const fs::path fpath = std::string(fname);
std::error_code ec;
if(!fs::exists(fpath, ec))
{
return false;
}
auto optional_f = util::OpenFileStream< std::ifstream >(
fpath, std::ios::in | std::ios::binary);
if(!optional_f)
return false;
auto& f = optional_f.value();
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 == size())
{
// is raw buffer
std::copy_n(std::istreambuf_iterator< char >(f), sz, begin());
return true;
}
std::array< byte_t, 128 > tmp;
llarp_buffer_t buf(tmp);
if(sz > sizeof(tmp))
{
return false;
}
f.read(reinterpret_cast< char* >(tmp.data()), sz);
return BDecode(&buf);
}
bool
SecretKey::SaveToFile(const char* fname) const
{
std::array< byte_t, 128 > tmp;
llarp_buffer_t buf(tmp);
if(!BEncode(&buf))
{
return false;
}
const fs::path fpath = std::string(fname);
auto optional_f = llarp::util::OpenFileStream< std::ofstream >(
fpath, std::ios::binary | std::ios::out);
if(!optional_f)
return false;
auto& f = optional_f.value();
if(!f.is_open())
return false;
f.write((char*)buf.base, buf.cur - buf.base);
return true;
}
bool
IdentitySecret::LoadFromFile(const char* fname)
{
const fs::path fpath = std::string(fname);
auto optional = util::OpenFileStream< std::ifstream >(
fpath, std::ios::binary | std::ios::in);
if(!optional)
return false;
auto& f = optional.value();
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;
}
byte_t*
Signature::R()
{
return data();
}
const byte_t*
Signature::R() const
{
return data();
}
byte_t*
Signature::C()
{
return data() + 32;
}
const byte_t*
Signature::C() const
{
return data() + 32;
}
} // namespace llarp