Implemented config and crypto infrastructure (#2)

Added config file read/write infrastructure with RapidJSON.
Added key pair generation and sign/verify helper with libsodium.
This commit is contained in:
Ravin Perera
2019-09-26 19:05:23 +05:30
committed by GitHub
parent d7eac1a1f1
commit 0382475de1
46 changed files with 16929 additions and 4 deletions

61
src/conf.cpp Normal file
View File

@@ -0,0 +1,61 @@
#include <cstdio>
#include <iostream>
#include <fstream>
#include <unistd.h>
#include "lib/rapidjson/document.h"
#include "lib/rapidjson/istreamwrapper.h"
#include "lib/rapidjson/ostreamwrapper.h"
#include "lib/rapidjson/writer.h"
#include "conf.h"
using namespace std;
using namespace rapidjson;
namespace conf
{
static const char * configPath;
string get_full_path(const char *filename)
{
string fullpath = configPath;
fullpath += filename;
return fullpath;
}
void load(const char *filename, Document &d)
{
ifstream ifs(get_full_path(filename));
IStreamWrapper isw(ifs);
d.ParseStream(isw);
}
void save(const char *filename, Document &d)
{
ofstream ofs(get_full_path(filename));
OStreamWrapper osw(ofs);
Writer<OStreamWrapper> writer(osw);
d.Accept(writer);
}
string get_exec_path()
{
char buf[PATH_MAX + 1];
if (readlink("/proc/self/exe", buf, sizeof(buf) - 1) == -1)
throw string("readlink() failed");
string str(buf);
return str.substr(0, str.rfind('/') + 1);
}
int init()
{
string execPath = get_exec_path();
char * confPathChar = (char *)malloc(execPath.size() + 1);
strcpy(confPathChar, &execPath[0]);
configPath = confPathChar;
return 1;
}
} // namespace conf