From ce3342d961362f3fbcb0cded7e61194cfaf660d1 Mon Sep 17 00:00:00 2001 From: _ <> Date: Tue, 19 Jan 2021 23:01:17 +0000 Subject: [PATCH] :recycle: refactor: splitting into in-mem and on-disk formats, for 'check key' cmd --- bare_minimum_crypto/cpp/signing_key.cpp | 19 ++++++++++++++----- bare_minimum_crypto/cpp/signing_key.h | 11 +++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/bare_minimum_crypto/cpp/signing_key.cpp b/bare_minimum_crypto/cpp/signing_key.cpp index 4d3b3be..112d9b2 100644 --- a/bare_minimum_crypto/cpp/signing_key.cpp +++ b/bare_minimum_crypto/cpp/signing_key.cpp @@ -9,6 +9,15 @@ namespace BareMinimumCrypto { using nlohmann::json; + vector SigningKeyFile::to_msgpack () const { + const auto j = json { + {"salt", json::binary (salt)}, + {"time_created", time_created.x}, + {"pubkey", json::binary (pubkey)}, + }; + return json::to_msgpack (j); + } + // The whole process for a passphrased key is like this: // Passphrase + random salt --> // Seed --> @@ -46,12 +55,12 @@ namespace BareMinimumCrypto { return nullopt; } - const auto j = json { - {"salt", json::binary (salt)}, - {"time_created", Instant::now ().x}, - {"pubkey", json::binary (key.pk)}, + SigningKeyFile key_on_disk { + salt, + Instant::now (), + key.pk }; - const auto msg = json::to_msgpack (j); + const auto msg = key_on_disk.to_msgpack (); ofstream f; f.open (file_path, ofstream::binary); diff --git a/bare_minimum_crypto/cpp/signing_key.h b/bare_minimum_crypto/cpp/signing_key.h index f4c841f..bf987d5 100644 --- a/bare_minimum_crypto/cpp/signing_key.h +++ b/bare_minimum_crypto/cpp/signing_key.h @@ -11,6 +11,15 @@ namespace BareMinimumCrypto { using namespace std; + struct SigningKeyFile { + vector salt; + Instant time_created; + vector pubkey; + + vector to_msgpack () const; + static optional try_from_msgpack (const vector & msg); + }; + struct SigningKey { vector pk; vector sk; @@ -25,6 +34,8 @@ namespace BareMinimumCrypto { static optional load_from_file (const string & file_path, const string & passphrase); + static optional check_file (const string & file_path); + vector pubkey () const; vector pub_to_msgpack () const;