Files
funstraw/crypto.h

59 lines
1.5 KiB
C
Raw Normal View History

#include <event2/bufferevent.h>
#include <event2/event.h>
#include <event2/listener.h>
2015-02-05 16:36:25 +00:00
2015-02-05 12:55:48 +00:00
#include <string>
class CryptoBase {
public:
virtual ~CryptoBase() {};
2015-02-05 16:36:25 +00:00
static void GenKey(std::string* key);
static void GenKeyPair(std::string* secret_key, std::string* public_key);
2015-02-05 16:36:25 +00:00
};
class CryptoPubServerConnection : public CryptoBase {
2015-02-05 16:36:25 +00:00
public:
2015-02-07 17:07:31 +01:00
CryptoPubServerConnection(struct bufferevent* bev, const std::string& secret_key);
~CryptoPubServerConnection();
static void OnReadable(struct bufferevent* bev, void* this__);
static void OnError(struct bufferevent* bev, const short what, void* this__);
2015-02-05 16:36:25 +00:00
private:
struct bufferevent* bev_;
2015-02-05 16:36:25 +00:00
const std::string secret_key_;
const std::string ephemeral_secret_key_;
const std::string client_public_key_;
enum {
AWAITING_HANDSHAKE,
READY,
} state_;
2015-02-05 16:36:25 +00:00
};
class CryptoPubServer : public CryptoBase {
public:
2015-02-07 17:07:31 +01:00
CryptoPubServer(const std::string& secret_key);
2015-02-05 16:36:25 +00:00
~CryptoPubServer();
static void OnNewConn(struct evconnlistener* listener, int fd, struct sockaddr* client_addr, int client_addrlen, void* this__);
2015-02-05 16:36:25 +00:00
void Loop();
private:
2015-02-07 17:07:31 +01:00
struct event_base* event_base_;
struct evconnlistener* listener_;
2015-02-05 16:36:25 +00:00
const std::string secret_key_;
2015-02-05 12:55:48 +00:00
};
2015-02-07 17:07:31 +01:00
class CryptoPubClient : public CryptoBase {
public:
CryptoPubClient(struct sockaddr* addr, socklen_t addrlen);
~CryptoPubClient();
static CryptoPubClient* FromHostname(const std::string& server_address, const std::string& server_port);
void Loop();
private:
struct event_base* event_base_;
struct bufferevent* bev_;
};