Clean up key file save and load.
This commit is contained in:
@@ -1,13 +1,4 @@
|
|||||||
#include <fstream>
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include <netdb.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
|
|
||||||
#include "crypto.h"
|
#include "crypto.h"
|
||||||
|
|
||||||
@@ -48,24 +39,10 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string secret_key;
|
std::string secret_key;
|
||||||
{
|
CryptoBase::ReadKeyFromFile(secret_key_filename, &secret_key);
|
||||||
std::fstream secret_key_file(secret_key_filename, std::fstream::in);
|
|
||||||
if (secret_key_file.fail()) {
|
|
||||||
std::cerr << "Failed to open secret key file" << std::endl;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
secret_key_file >> secret_key;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string server_public_key;
|
std::string server_public_key;
|
||||||
{
|
CryptoBase::ReadKeyFromFile(server_public_key_filename, &server_public_key);
|
||||||
std::fstream server_public_key_file(server_public_key_filename, std::fstream::in);
|
|
||||||
if (server_public_key_file.fail()) {
|
|
||||||
std::cerr << "Failed to open server public key file" << std::endl;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
server_public_key_file >> server_public_key;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto client = CryptoPubClient::FromHostname(server_address, server_port, secret_key, server_public_key);
|
auto client = CryptoPubClient::FromHostname(server_address, server_port, secret_key, server_public_key);
|
||||||
client->Loop();
|
client->Loop();
|
||||||
|
|||||||
@@ -1,12 +1,4 @@
|
|||||||
#include <fstream>
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
|
|
||||||
#include "crypto.h"
|
#include "crypto.h"
|
||||||
|
|
||||||
@@ -28,14 +20,7 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string secret_key;
|
std::string secret_key;
|
||||||
{
|
CryptoBase::ReadKeyFromFile(secret_key_filename, &secret_key);
|
||||||
std::fstream secret_key_file(secret_key_filename, std::fstream::in);
|
|
||||||
if (secret_key_file.fail()) {
|
|
||||||
std::cerr << "Failed to open secret key file" << std::endl;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
secret_key_file >> secret_key;
|
|
||||||
}
|
|
||||||
|
|
||||||
CryptoPubServer server(secret_key);
|
CryptoPubServer server(secret_key);
|
||||||
server.Loop();
|
server.Loop();
|
||||||
|
|||||||
13
crypto.cc
13
crypto.cc
@@ -4,6 +4,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include <sodium/crypto_box.h>
|
#include <sodium/crypto_box.h>
|
||||||
@@ -46,6 +47,18 @@ void CryptoBase::DerivePublicKey(const std::string& secret_key, std::string* pub
|
|||||||
public_key->assign((char*)buf, crypto_box_PUBLICKEYBYTES);
|
public_key->assign((char*)buf, crypto_box_PUBLICKEYBYTES);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CryptoBase::ReadKeyFromFile(const std::string& filename, std::string* key) {
|
||||||
|
std::fstream key_file(filename, std::fstream::in);
|
||||||
|
assert(!key_file.fail());
|
||||||
|
key_file >> *key;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CryptoBase::WriteKeyToFile(const std::string& filename, const std::string& key) {
|
||||||
|
std::fstream key_file(filename, std::fstream::out);
|
||||||
|
assert(!key_file.fail());
|
||||||
|
key_file << key;
|
||||||
|
}
|
||||||
|
|
||||||
void CryptoBase::EncodeEncryptAppend(const std::string& secret_key, const std::string& public_key, const TLVNode& input, TLVNode* container) {
|
void CryptoBase::EncodeEncryptAppend(const std::string& secret_key, const std::string& public_key, const TLVNode& input, TLVNode* container) {
|
||||||
assert(secret_key.length() == crypto_box_SECRETKEYBYTES);
|
assert(secret_key.length() == crypto_box_SECRETKEYBYTES);
|
||||||
assert(public_key.length() == crypto_box_PUBLICKEYBYTES);
|
assert(public_key.length() == crypto_box_PUBLICKEYBYTES);
|
||||||
|
|||||||
3
crypto.h
3
crypto.h
@@ -13,6 +13,9 @@ class CryptoBase {
|
|||||||
static void GenKey(std::string* key);
|
static void GenKey(std::string* key);
|
||||||
static void GenKeyPair(std::string* secret_key, std::string* public_key);
|
static void GenKeyPair(std::string* secret_key, std::string* public_key);
|
||||||
static void DerivePublicKey(const std::string& secret_key, std::string* public_key);
|
static void DerivePublicKey(const std::string& secret_key, std::string* public_key);
|
||||||
|
static void ReadKeyFromFile(const std::string& filename, std::string* key);
|
||||||
|
static void WriteKeyToFile(const std::string& filename, const std::string& key);
|
||||||
|
|
||||||
static void EncodeEncryptAppend(const std::string& secret_key, const std::string& public_key, const TLVNode& input, TLVNode* container);
|
static void EncodeEncryptAppend(const std::string& secret_key, const std::string& public_key, const TLVNode& input, TLVNode* container);
|
||||||
TLVNode *DecryptDecode(const std::string& secret_key, const std::string& public_key, const TLVNode& input);
|
TLVNode *DecryptDecode(const std::string& secret_key, const std::string& public_key, const TLVNode& input);
|
||||||
|
|
||||||
|
|||||||
@@ -13,14 +13,7 @@ int main(int argc, char *argv[]) {
|
|||||||
std::string key;
|
std::string key;
|
||||||
CryptoBase::GenKey(&key);
|
CryptoBase::GenKey(&key);
|
||||||
|
|
||||||
{
|
CryptoBase::WriteKeyToFile(argv[1], key);
|
||||||
std::fstream key_file(argv[1], std::fstream::out);
|
|
||||||
if (key_file.fail()) {
|
|
||||||
std::cerr << "Failed to open key file" << std::endl;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
key_file << key;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,23 +13,8 @@ int main(int argc, char *argv[]) {
|
|||||||
std::string secret_key, public_key;
|
std::string secret_key, public_key;
|
||||||
CryptoBase::GenKeyPair(&secret_key, &public_key);
|
CryptoBase::GenKeyPair(&secret_key, &public_key);
|
||||||
|
|
||||||
{
|
CryptoBase::WriteKeyToFile(argv[1], secret_key);
|
||||||
std::fstream secret_key_file(argv[1], std::fstream::out);
|
CryptoBase::WriteKeyToFile(argv[2], public_key);
|
||||||
if (secret_key_file.fail()) {
|
|
||||||
std::cerr << "Failed to open secret key file" << std::endl;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
secret_key_file << secret_key;
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
std::fstream public_key_file(argv[2], std::fstream::out);
|
|
||||||
if (public_key_file.fail()) {
|
|
||||||
std::cerr << "Failed to open public key file" << std::endl;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
public_key_file << public_key;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user