From cdd059370da5cb0a9d71c855871888264e28fab0 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Sat, 7 Feb 2015 13:38:51 -0800 Subject: [PATCH] Log client ID from server --- crypto.cc | 13 ++++++++++++- crypto.h | 2 ++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/crypto.cc b/crypto.cc index af0f7e7..e44bc3e 100644 --- a/crypto.cc +++ b/crypto.cc @@ -26,6 +26,17 @@ #define TLV_TYPE_SERVER_HANDSHAKE_SECURE 0x8002 +std::string CryptoBase::BinToHex(const std::string& bin) { + static const char hex[] = "0123456789abcdef"; + std::string ret; + ret.reserve(bin.length() * 2); + for (int i = 0; i < bin.length(); i++) { + ret.push_back(hex[(bin[i] & 0xf0) >> 4]); + ret.push_back(hex[bin[i] & 0x0f]); + } + return ret; +} + void CryptoBase::GenKey(std::string* key) { unsigned char buf[crypto_secretbox_KEYBYTES]; randombytes_buf(buf, crypto_secretbox_KEYBYTES); @@ -243,7 +254,7 @@ void CryptoPubServerConnection::OnHandshake(const std::string& input) { bufferevent_write(bev_, out.data(), out.length()); this->state_ = READY; - Log() << "Handshake successful" << std::endl; + Log() << "Handshake successful (client ID: " << BinToHex(client_public_key_) << ")" << std::endl; } void CryptoPubServerConnection::OnError_(struct bufferevent* bev, const short what, void* this__) { diff --git a/crypto.h b/crypto.h index bc0de32..e9a9e56 100644 --- a/crypto.h +++ b/crypto.h @@ -10,6 +10,8 @@ class CryptoBase { public: virtual ~CryptoBase() {}; + static std::string BinToHex(const std::string& bin); + static void GenKey(std::string* key); static void GenKeyPair(std::string* secret_key, std::string* public_key); static void DerivePublicKey(const std::string& secret_key, std::string* public_key);