~metalfaceee/sodaboy

bf431a4ae18227f7ccca847a4123ac2051bd2fb5 — daniel mills 2 months ago master
init
3 files changed, 133 insertions(+), 0 deletions(-)

A KeyGenerator.cpp
A SodaBoy.cpp
A main.cpp
A  => KeyGenerator.cpp +83 -0
@@ 1,83 @@
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <sodium.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include "boost/filesystem.hpp"

#define SODA_BOY_HOME_DIR "/.sodaboy"

namespace fs = boost::filesystem;

class KeyGenerator {
public:
  KeyGenerator() {
    this->set_home_dir();

    if(this->keys_exist()) {
      this->load_pair();
    } else {
      this->generate_pair();
    }
  }
  ~KeyGenerator() {};

  unsigned char* get_public() { return this->m_publickey; }
  unsigned char* get_secret() { return this->m_secretkey; }

private:
  std::string m_homedir;
  unsigned char m_publickey[crypto_box_PUBLICKEYBYTES];
  unsigned char m_secretkey[crypto_box_SECRETKEYBYTES];

  void set_home_dir() {
    const char *homedir;
    if ((homedir = getenv("HOME")) == NULL) {
      homedir = getpwuid(getuid())->pw_dir;
    }
    std::ostringstream oss;
    oss << homedir << SODA_BOY_HOME_DIR;
    m_homedir = oss.str();
  }

  bool keys_exist() {
    return fs::exists(this->m_homedir);
  }

  void load_pair() {
    std::string p = this->m_homedir + "/pub.key";
    std::string s = this->m_homedir + "/secret.key";

    std::ifstream ifs (p, std::ifstream::in);
    ifs >> this->m_publickey;
    ifs.close();

    std::ifstream ifs2 (s, std::ifstream::in);
    ifs2 >> this->m_secretkey;
    ifs2.close();
  }

  void generate_pair() {
    fs::create_directory(this->m_homedir);
    crypto_box_keypair(this->m_publickey, this->m_secretkey);
    this->write_key_pair_to_file();
  }

  void write_key(std::string filename, unsigned char* value) {
    std::ofstream ofs (filename, std::ofstream::out);
    ofs << value;
    ofs.close();
  }

  void write_key_pair_to_file() {
    std::string p = this->m_homedir + "/pub.key";
    std::string s = this->m_homedir + "/secret.key";

    this->write_key(p, this->m_publickey);

    this->write_key(s, this->m_secretkey);
  }
};

A  => SodaBoy.cpp +36 -0
@@ 1,36 @@
#include <iostream>
#include <string>
#include "./KeyGenerator.cpp"
#define SODA_BOY_VERSION "0.1"

class SodaBoy {
public:
  SodaBoy() {
  }
  ~SodaBoy() {}

  void run() {
    this->m_running = true;
    std::cout << "sodaboy v"<< SODA_BOY_VERSION << " started. \n";
    // std::cout << m_keygen.get_public() << " : " << m_keygen.get_secret() << "\n";
    while(this->m_running) {
      this->print_header();
      std::cin >> m_input;

      if(m_input == "q") {
        std::cout << "Quitting\n";
        this->m_running = false;
      }
    }
  }

private:
  KeyGenerator m_keygen;
  bool m_running;
  std::string m_input;

  void print_header() {
    std::cout << "\n\nCommands:\n\t QUIT: quits application\n\n";
  }
};


A  => main.cpp +14 -0
@@ 1,14 @@
#include <iostream>
#include <sodium.h>
#include "./SodaBoy.cpp"
int main(void)
{
    if (sodium_init() < 0) {
        std::cerr << "Unable to load libsodium aborting.\n";
        return -1;
    }

    SodaBoy runboy;
    runboy.run();
    return 0;
}