diff --git a/bare_minimum_crypto/cpp/bmc_test.cpp b/bare_minimum_crypto/cpp/bmc_test.cpp index 56c369c..de1cf8e 100644 --- a/bare_minimum_crypto/cpp/bmc_test.cpp +++ b/bare_minimum_crypto/cpp/bmc_test.cpp @@ -7,8 +7,6 @@ #include -// From https://github.com/tkislan/base64 -#include "cpp-base64/base64.h" #include "json.hpp" #include "string_helpers.h" @@ -81,7 +79,7 @@ optional try_verify_signed_data ( const string purpose = j ["purpose"]; const string payload_b64 = j ["payload_b64"]; - const auto payload = std::move (*b64_decode (payload_b64)); + const auto payload = std::move (*base64_decode (payload_b64)); return VerifiedData { payload, @@ -169,41 +167,6 @@ public: } }; -int check_base64 () { - // I assume that char is 8 bits - // char is C++ nonsense inherited from C nonsense - if (sizeof (char) != sizeof (uint8_t)) { - cerr << "char is not the same size as uint8_t" << endl; - return 1; - } - - vector v {1, 2, 3, 4, 5, 6}; - const auto s = base64_encode (v); - - if (s != "AQIDBAUG") { - cerr << "Base64 encoding failed" << endl; - return 1; - } - - // Trivial decode - const auto v2 = std::move (*b64_decode (s)); - - if (v2 != v) { - cerr << "Base64 trivial decode failed" << endl; - return 1; - } - - // Decode should fail - const auto v3 = b64_decode ("AQIDBAUG."); - - if (v3 != nullopt) { - cerr << "Base64 decode should have failed" << endl; - return 1; - } - - return 0; -} - int happy_path () { // We generate a root key and keep it somewhere safe // (offline, hopefully) @@ -297,7 +260,7 @@ int happy_path () { } int main () { - if (check_base64 () != 0) { + if (test_base64 () != 0) { return 1; } diff --git a/bare_minimum_crypto/cpp/string_helpers.cpp b/bare_minimum_crypto/cpp/string_helpers.cpp index 4cd015d..711c95a 100644 --- a/bare_minimum_crypto/cpp/string_helpers.cpp +++ b/bare_minimum_crypto/cpp/string_helpers.cpp @@ -1,5 +1,7 @@ #include "string_helpers.h" +#include + #include "cpp-base64/base64.h" namespace BareMinimumCrypto { @@ -13,13 +15,48 @@ namespace BareMinimumCrypto { return ::base64_encode (string_view ((const char *)v.data (), v.size ())); } - optional > b64_decode (const string & s) { + optional > base64_decode (const string & s) { try { - const auto decoded = base64_decode (s); + const auto decoded = ::base64_decode (s); return copy_to_bytes (decoded); } catch (const exception &) { return nullopt; } } + + int test_base64 () { + // I assume that char is 8 bits + // char is C++ nonsense inherited from C nonsense + if (sizeof (char) != sizeof (uint8_t)) { + cerr << "char is not the same size as uint8_t" << endl; + return 1; + } + + vector v {1, 2, 3, 4, 5, 6}; + const auto s = base64_encode (v); + + if (s != "AQIDBAUG") { + cerr << "Base64 encoding failed" << endl; + return 1; + } + + // Trivial decode + const auto v2 = std::move (*base64_decode (s)); + + if (v2 != v) { + cerr << "Base64 trivial decode failed" << endl; + return 1; + } + + // Decode should fail + const auto v3 = base64_decode ("AQIDBAUG."); + + if (v3 != nullopt) { + cerr << "Base64 decode should have failed" << endl; + return 1; + } + + return 0; + } } diff --git a/bare_minimum_crypto/cpp/string_helpers.h b/bare_minimum_crypto/cpp/string_helpers.h index 201534c..b073429 100644 --- a/bare_minimum_crypto/cpp/string_helpers.h +++ b/bare_minimum_crypto/cpp/string_helpers.h @@ -12,5 +12,7 @@ namespace BareMinimumCrypto { // Not sure why the Base64 lib fails to provide this API string base64_encode (const vector & v); - optional > b64_decode (const string & s); + optional > base64_decode (const string & s); + + int test_base64 (); }