1414
1515#include " app/src/secure/user_secure_manager.h"
1616
17+ #include " app/src/base64.h"
1718#include " app/src/callback.h"
1819#include " app/src/secure/user_secure_internal.h"
1920
@@ -226,8 +227,9 @@ static uint8_t HexToValue(char digit) {
226227}
227228
228229// A single character at the start of the encoding specifies how it's encoded,
229- // in case we change to Base64/etc. in the future.
230+ // in case we change to different formats in the future.
230231static const char kHeaderHexEncoded = ' $' ;
232+ static const char kHeaderBase64Encoded = ' #' ;
231233
232234bool UserSecureManager::AsciiToBinary (const std::string& encoded,
233235 std::string* decoded) {
@@ -254,6 +256,8 @@ bool UserSecureManager::AsciiToBinary(const std::string& encoded,
254256 (*decoded)[d] = (HexToValue (hi) << 4 ) | HexToValue (lo);
255257 }
256258 return true ;
259+ } else if (encoded[0 ] == kHeaderBase64Encoded ) {
260+ return internal::Base64Decode (encoded.substr (1 ), decoded);
257261 } else {
258262 // Unknown header byte, can't decode.
259263 *decoded = std::string ();
@@ -264,17 +268,13 @@ bool UserSecureManager::AsciiToBinary(const std::string& encoded,
264268void UserSecureManager::BinaryToAscii (const std::string& original,
265269 std::string* encoded) {
266270 FIREBASE_ASSERT (encoded != nullptr );
267- encoded->resize (1 + original.length () * 2 );
268-
269- // Emit header byte to signify hex encoding.
270- (*encoded)[0 ] = kHeaderHexEncoded ;
271- for (int o = 0 , e = 1 ; e < encoded->length (); ++o, e += 2 ) {
272- unsigned char value = original[o];
273- unsigned char hi = (value & 0xF0 ) >> 4 ;
274- unsigned char lo = (value & 0x0F ) >> 0 ;
275- // First byte is the header, so add 1.
276- (*encoded)[e + 0 ] = (hi < 10 ) ? (' 0' + hi) : (' A' + hi - 10 );
277- (*encoded)[e + 1 ] = (lo < 10 ) ? (' 0' + lo) : (' A' + lo - 10 );
271+
272+ // Use base64 encoding.
273+ std::string base64;
274+ if (internal::Base64Encode (original, &base64)) {
275+ *encoded = std::string () + kHeaderBase64Encoded + base64;
276+ } else {
277+ *encoded = std::string ();
278278 }
279279}
280280
0 commit comments