diff --git a/include/olm/crypto.hh b/include/olm/crypto.hh index 7a05f8d..159bac7 100644 --- a/include/olm/crypto.hh +++ b/include/olm/crypto.hh @@ -17,6 +17,7 @@ #include #include +#include namespace olm { @@ -26,6 +27,7 @@ static const std::size_t IV_LENGTH = 16; struct Curve25519PublicKey { std::uint8_t public_key[KEY_LENGTH]; + std::string to_string() const; }; diff --git a/include/olm/memory.hh b/include/olm/memory.hh index 74ff9f8..89afd99 100644 --- a/include/olm/memory.hh +++ b/include/olm/memory.hh @@ -87,4 +87,23 @@ std::uint8_t * store_array( return destination + sizeof(T); } +/** convert an array of bytes to a string representation */ +template +std::string bytes_to_string(T start, T end) { + std::ostringstream ss; + ss << std::hex << std::setfill('0'); + while (start != end) { + ss << std::setw(2) << static_cast(*start++); + if (start != end) { + ss << ":"; + } + } + return ss.str(); +} + +template +std::string bytes_to_string(T start, size_t len) { + return bytes_to_string(start, start+len); +} + } // namespace olm diff --git a/src/crypto.cpp b/src/crypto.cpp index da1d617..3801e93 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -101,6 +101,11 @@ inline static void hmac_sha256_final( } // namespace +std::string olm::Curve25519PublicKey::to_string() const { + return olm::bytes_to_string(std::begin(public_key), + std::end(public_key)); +}; + void olm::curve25519_generate_key( std::uint8_t const * random_32_bytes, olm::Curve25519KeyPair & key_pair