♻️ refactor
parent
8438ec3225
commit
0fa43a71d2
|
@ -7,8 +7,6 @@
|
|||
|
||||
#include <sodium.h>
|
||||
|
||||
// From https://github.com/tkislan/base64
|
||||
#include "cpp-base64/base64.h"
|
||||
#include "json.hpp"
|
||||
|
||||
#include "string_helpers.h"
|
||||
|
@ -81,7 +79,7 @@ optional <VerifiedData> 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 <uint8_t> 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;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
#include "string_helpers.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#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 <vector <uint8_t>> b64_decode (const string & s) {
|
||||
optional <vector <uint8_t>> 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 <uint8_t> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,5 +12,7 @@ namespace BareMinimumCrypto {
|
|||
|
||||
// Not sure why the Base64 lib fails to provide this API
|
||||
string base64_encode (const vector <uint8_t> & v);
|
||||
optional <vector <uint8_t>> b64_decode (const string & s);
|
||||
optional <vector <uint8_t>> base64_decode (const string & s);
|
||||
|
||||
int test_base64 ();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue