Public key generation
This commit is contained in:
13
Makefile
13
Makefile
@@ -1,7 +1,10 @@
|
||||
all: auth-server gen-pubkeypair
|
||||
all: auth-server gen-keypair
|
||||
|
||||
auth-server: auth-server.cc
|
||||
g++ -o auth-server auth-server.cc nacl/build/instance1/lib/amd64/randombytes.o nacl/build/instance1/lib/amd64/libnacl.a
|
||||
%.o: %.cc
|
||||
g++ -c -o $@ $<
|
||||
|
||||
gen-pubkeypair: gen-pubkeypair.cc
|
||||
g++ -o gen-pubkeypair gen-pubkeypair.cc nacl/build/instance1/lib/amd64/randombytes.o nacl/build/instance1/lib/amd64/libnacl.a
|
||||
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-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
|
||||
|
||||
19
crypto.cc
Normal file
19
crypto.cc
Normal file
@@ -0,0 +1,19 @@
|
||||
#include "crypto.h"
|
||||
|
||||
#include "nacl/build/instance1/include/amd64/crypto_box.h"
|
||||
|
||||
std::string CryptoBase::BinToHex(const std::string& bin) {
|
||||
static const char *hex = "0123456789ABCDEF";
|
||||
std::string ret;
|
||||
ret.reserve(bin.length() * 2);
|
||||
for (size_t i = 0; i < bin.length(); i++) {
|
||||
const char c = bin[i];
|
||||
ret.push_back(hex[(c & 0xf0) >> 4]);
|
||||
ret.push_back(hex[c & 0x0f]);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void CryptoBase::GenKeyPair(std::string* sk, std::string* pk) {
|
||||
*pk = crypto_box_keypair(sk);
|
||||
}
|
||||
7
crypto.h
Normal file
7
crypto.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#include <string>
|
||||
|
||||
class CryptoBase {
|
||||
public:
|
||||
static std::string BinToHex(const std::string& bin);
|
||||
static void GenKeyPair(std::string* sk, std::string* pk);
|
||||
};
|
||||
38
gen-keypair.cc
Normal file
38
gen-keypair.cc
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <ctime>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
#include "crypto.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 3) {
|
||||
std::cerr << "Usage: " << argv[0] << " secret_filename public_filename" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string pk;
|
||||
std::string sk;
|
||||
CryptoBase::GenKeyPair(&sk, &pk);
|
||||
|
||||
{
|
||||
std::fstream skf(argv[1], std::fstream::out);
|
||||
if (skf.fail()) {
|
||||
std::cerr << "Failed to open secret key file" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
skf << "# Secret key" << std::endl;
|
||||
skf << CryptoBase::BinToHex(sk) << std::endl;
|
||||
}
|
||||
|
||||
{
|
||||
std::fstream pkf(argv[2], std::fstream::out);
|
||||
if (pkf.fail()) {
|
||||
std::cerr << "Failed to open public key file" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
pkf << "# Public key" << std::endl;
|
||||
pkf << CryptoBase::BinToHex(pk) << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "nacl/build/instance1/include/amd64/crypto_box.h"
|
||||
|
||||
class CryptoBase {
|
||||
public:
|
||||
static const std::string BinToHex(const std::string& bin) {
|
||||
static const char *hex = "0123456789ABCDEF";
|
||||
std::string ret;
|
||||
ret.reserve(bin.length() * 2);
|
||||
for (size_t i = 0; i < bin.length(); i++) {
|
||||
const char c = bin[i];
|
||||
ret.push_back(hex[(c & 0xf0) >> 4]);
|
||||
ret.push_back(hex[c & 0x0f]);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
std::string pk;
|
||||
std::string sk;
|
||||
|
||||
pk = crypto_box_keypair(&sk);
|
||||
|
||||
std::cout << "Secret: " << CryptoBase::BinToHex(sk) << std::endl;
|
||||
std::cout << "Public: " << CryptoBase::BinToHex(pk) << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user