From 9c4fe4a26ede4cc8a72d8cc4b9d73a41840d602f Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Sun, 17 Jan 2021 15:45:59 -0600 Subject: [PATCH] :recycle: refactor --- bare_minimum_crypto/cpp/Makefile | 7 +++++-- bare_minimum_crypto/cpp/bmc_test.cpp | 13 +------------ bare_minimum_crypto/cpp/time_helpers.cpp | 13 +++++++++++++ bare_minimum_crypto/cpp/time_helpers.h | 11 +++++++++++ 4 files changed, 30 insertions(+), 14 deletions(-) create mode 100644 bare_minimum_crypto/cpp/time_helpers.cpp create mode 100644 bare_minimum_crypto/cpp/time_helpers.h diff --git a/bare_minimum_crypto/cpp/Makefile b/bare_minimum_crypto/cpp/Makefile index 2271b72..e958479 100644 --- a/bare_minimum_crypto/cpp/Makefile +++ b/bare_minimum_crypto/cpp/Makefile @@ -5,10 +5,10 @@ CXX_FLAGS=$(FLAGS) -c L_FLAGS=$(FLAGS) LIBS=-lsodium -bmc_test: bmc_test.o base64.o string_helpers.o +bmc_test: bmc_test.o base64.o string_helpers.o time_helpers.o $(CXX) -o $@ $^ $(L_FLAGS) $(LIBS) -bmc_test.o: bmc_test.cpp string_helpers.h +bmc_test.o: bmc_test.cpp string_helpers.h time_helpers.h $(CXX) -o $@ $(CXX_FLAGS) $< base64.o: cpp-base64/base64.cpp cpp-base64/base64.h @@ -17,6 +17,9 @@ base64.o: cpp-base64/base64.cpp cpp-base64/base64.h string_helpers.o: string_helpers.cpp string_helpers.h cpp-base64/base64.h $(CXX) -o $@ $(CXX_FLAGS) $< +time_helpers.o: time_helpers.cpp time_helpers.h + $(CXX) -o $@ $(CXX_FLAGS) $< + clean: rm -f bmc_test bmc_test.o base64.o string_helpers.o diff --git a/bare_minimum_crypto/cpp/bmc_test.cpp b/bare_minimum_crypto/cpp/bmc_test.cpp index e5c9f17..000ab94 100644 --- a/bare_minimum_crypto/cpp/bmc_test.cpp +++ b/bare_minimum_crypto/cpp/bmc_test.cpp @@ -12,19 +12,13 @@ #include "json.hpp" #include "string_helpers.h" +#include "time_helpers.h" using namespace std; -using chrono::duration_cast; -using chrono::seconds; -using chrono::system_clock; using nlohmann::json; using namespace BareMinimumCrypto; -const int64_t about_1_week = (int64_t)7 * 86400; -const int64_t about_3_months = (int64_t)105 * 86400; -const int64_t about_1_year = (int64_t)365 * 86400; - struct ExpiringSignature { string cert_s; vector sig; @@ -42,11 +36,6 @@ struct ExpiringSignature { } }; -int64_t get_seconds_since_epoch () { - const auto utc_now = system_clock::now (); - return duration_cast (utc_now.time_since_epoch ()).count (); -} - void try_sodium_init () { if (sodium_init () < 0) { throw std::runtime_error ("Can't initialize libsodium"); diff --git a/bare_minimum_crypto/cpp/time_helpers.cpp b/bare_minimum_crypto/cpp/time_helpers.cpp new file mode 100644 index 0000000..adee6ec --- /dev/null +++ b/bare_minimum_crypto/cpp/time_helpers.cpp @@ -0,0 +1,13 @@ +#include "time_helpers.h" + +#include + +namespace BareMinimumCrypto { + using namespace std; + using namespace chrono; + + int64_t get_seconds_since_epoch () { + const auto utc_now = system_clock::now (); + return duration_cast (utc_now.time_since_epoch ()).count (); + } +} diff --git a/bare_minimum_crypto/cpp/time_helpers.h b/bare_minimum_crypto/cpp/time_helpers.h new file mode 100644 index 0000000..763aa3c --- /dev/null +++ b/bare_minimum_crypto/cpp/time_helpers.h @@ -0,0 +1,11 @@ +#pragma once + +#include + +namespace BareMinimumCrypto { + const int64_t about_1_week = (int64_t)7 * 86400; + const int64_t about_3_months = (int64_t)105 * 86400; + const int64_t about_1_year = (int64_t)365 * 86400; + + int64_t get_seconds_since_epoch (); +}