ptth/bare_minimum_crypto/cpp/string_helpers.cpp

26 lines
603 B
C++

#include "string_helpers.h"
#include "cpp-base64/base64.h"
namespace BareMinimumCrypto {
using namespace std;
vector <uint8_t> copy_to_bytes (const string & s) {
return vector <uint8_t> ((const uint8_t *)&s [0], (const uint8_t *)&s [s.size ()]);
}
string base64_encode (const vector <uint8_t> & v) {
return ::base64_encode (string_view ((const char *)v.data (), v.size ()));
}
optional <vector <uint8_t>> b64_decode (const string & s) {
try {
const auto decoded = base64_decode (s);
return copy_to_bytes (decoded);
}
catch (const exception &) {
return nullopt;
}
}
}