14
14
15
15
#include " app/src/secure/user_secure_manager.h"
16
16
17
+ #include " app/src/base64.h"
17
18
#include " app/src/callback.h"
18
19
#include " app/src/secure/user_secure_internal.h"
19
20
@@ -226,8 +227,9 @@ static uint8_t HexToValue(char digit) {
226
227
}
227
228
228
229
// 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.
230
231
static const char kHeaderHexEncoded = ' $' ;
232
+ static const char kHeaderBase64Encoded = ' #' ;
231
233
232
234
bool UserSecureManager::AsciiToBinary (const std::string& encoded,
233
235
std::string* decoded) {
@@ -254,6 +256,8 @@ bool UserSecureManager::AsciiToBinary(const std::string& encoded,
254
256
(*decoded)[d] = (HexToValue (hi) << 4 ) | HexToValue (lo);
255
257
}
256
258
return true ;
259
+ } else if (encoded[0 ] == kHeaderBase64Encoded ) {
260
+ return internal::Base64Decode (encoded.substr (1 ), decoded);
257
261
} else {
258
262
// Unknown header byte, can't decode.
259
263
*decoded = std::string ();
@@ -264,17 +268,13 @@ bool UserSecureManager::AsciiToBinary(const std::string& encoded,
264
268
void UserSecureManager::BinaryToAscii (const std::string& original,
265
269
std::string* encoded) {
266
270
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 ();
278
278
}
279
279
}
280
280
0 commit comments