Add gen-key for shared secret key generation
This commit is contained in:
5
Makefile
5
Makefile
@@ -1,4 +1,4 @@
|
||||
all: auth-server gen-keypair
|
||||
all: auth-server gen-key gen-keypair
|
||||
|
||||
%.o: %.cc
|
||||
g++ -c -o $@ $<
|
||||
@@ -6,5 +6,8 @@ all: auth-server gen-keypair
|
||||
auth-server: auth-server.o crypto.o
|
||||
g++ -o auth-server auth-server.o crypto.o nacl/build/instance1/lib/amd64/randombytes.o nacl/build/instance1/lib/amd64/libnacl.a
|
||||
|
||||
gen-key: gen-key.o crypto.o
|
||||
g++ -o gen-key gen-key.o crypto.o nacl/build/instance1/lib/amd64/randombytes.o nacl/build/instance1/lib/amd64/libnacl.a
|
||||
|
||||
gen-keypair: gen-keypair.o crypto.o
|
||||
g++ -o gen-keypair gen-keypair.o crypto.o nacl/build/instance1/lib/amd64/randombytes.o nacl/build/instance1/lib/amd64/libnacl.a
|
||||
|
||||
12
crypto.cc
12
crypto.cc
@@ -1,6 +1,8 @@
|
||||
#include "crypto.h"
|
||||
|
||||
#include "nacl/build/instance1/include/amd64/crypto_box.h"
|
||||
#include "nacl/build/instance1/include/amd64/crypto_secretbox.h"
|
||||
#include "nacl/build/instance1/include/amd64/randombytes.h"
|
||||
|
||||
std::string CryptoBase::BinToHex(const std::string& bin) {
|
||||
static const char *hex = "0123456789ABCDEF";
|
||||
@@ -14,6 +16,12 @@ std::string CryptoBase::BinToHex(const std::string& bin) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
void CryptoBase::GenKeyPair(std::string* sk, std::string* pk) {
|
||||
*pk = crypto_box_keypair(sk);
|
||||
void CryptoBase::GenKey(std::string* key) {
|
||||
char buf[crypto_secretbox_KEYBYTES];
|
||||
randombytes((unsigned char *)buf, crypto_secretbox_KEYBYTES);
|
||||
*key = buf;
|
||||
}
|
||||
|
||||
void CryptoBase::GenKeyPair(std::string* secret_key, std::string* public_key) {
|
||||
*public_key = crypto_box_keypair(secret_key);
|
||||
}
|
||||
|
||||
3
crypto.h
3
crypto.h
@@ -3,5 +3,6 @@
|
||||
class CryptoBase {
|
||||
public:
|
||||
static std::string BinToHex(const std::string& bin);
|
||||
static void GenKeyPair(std::string* sk, std::string* pk);
|
||||
static void GenKey(std::string* key);
|
||||
static void GenKeyPair(std::string* secret_key, std::string* public_key);
|
||||
};
|
||||
|
||||
27
gen-key.cc
Normal file
27
gen-key.cc
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <ctime>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
#include "crypto.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 2) {
|
||||
std::cerr << "Usage: " << argv[0] << " key_filename" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string key;
|
||||
CryptoBase::GenKey(&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 << "# Shared secret key" << std::endl;
|
||||
key_file << CryptoBase::BinToHex(key) << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user