Funkce pro overeni Base64 kodovaneho retezce

This commit is contained in:
Pavel Brychta 2022-06-11 11:17:28 +02:00
parent ed8d1e5a03
commit 7ba28c9ba2
2 changed files with 38 additions and 9 deletions

View File

@ -3,6 +3,23 @@
#include <libb64/cdecode.h> #include <libb64/cdecode.h>
#include <cutils.hpp> #include <cutils.hpp>
bool isBase64(const char *p)
{
char c;
if (strlen(p) % 4 != 0)
return false;
do {
c = *p;
if (isalnum(c)) // alphanumberics are OK
continue;
if (c == '/' || c == '+' || c == '=') // + or / or = is OK.
continue;
return false;
} while (*++p != 0);
return true;
}
void strEncode(String &str) void strEncode(String &str)
{ {
@ -41,9 +58,11 @@ void strDecode(String &str)
uint32_t cid = ESP_getChipId(); uint32_t cid = ESP_getChipId();
uint32_t *pkey = (uint32_t *)&key[0]; uint32_t *pkey = (uint32_t *)&key[0];
uint8_t data[str.length()]; uint8_t data[str.length()];
unsigned int clen = base64_decode_chars(src, str.length(), (char *)data);
String result = F(""); String result = F("");
if (isBase64(str.c_str())) {
unsigned int clen = base64_decode_chars(src, str.length(), (char *)data);
if (0 != clen) { if (0 != clen) {
*pkey = cid; *pkey = cid;
for (i = 0; i < clen; i++) { for (i = 0; i < clen; i++) {
@ -54,5 +73,6 @@ void strDecode(String &str)
result.concat(char(b)); result.concat(char(b));
} }
} }
}
str = result; str = result;
} }

View File

@ -16,4 +16,13 @@ void strEncode(String &str);
*/ */
void strDecode(String &str); void strDecode(String &str);
/**
* @brief Overeni, ze je retezec Base64
*
* @param p Ukazatel na retezec
* @return true Retezec je kodovany pomoci Base64
* @return false Retezec neni kodovany pomoci Base64
*/
bool isBase64(const char *p);
#endif #endif