diff --git a/LICENSE b/LICENSE index 2071b23..d6c998d 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) +Copyright (c) 2019-2021 xPablo.cz Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/src/encString.cpp b/src/encString.cpp new file mode 100644 index 0000000..3c117fd --- /dev/null +++ b/src/encString.cpp @@ -0,0 +1,65 @@ +#include "encString.hpp" +#include +#include +#include + +#if defined(ESP8266) +# define ESP_getChipId() (ESP.getChipId()) +#else +# include +# define ESP_getChipId() ((uint32_t)ESP.getEfuseMac()) +#endif + +void strEncode(String &str) +{ + + const char *src = &str[0]; + uint8_t key[4]; + unsigned int enciphidx = 0; + char c; + unsigned int i; + uint32_t cid = ESP_getChipId(); + uint32_t *pkey = (uint32_t *)&key[0]; + uint8_t data[str.length()]; + + *pkey = cid; + for (i = 0; i < str.length(); i++) { + c = src[i]; + c ^= key[enciphidx]; + ++enciphidx; + enciphidx %= sizeof(key); + data[i] = c; + } +#if defined(ESP8266) + str = base64::encode(data, str.length(), false); +#else + str = base64::encode(data, str.length()); +#endif +} + +void strDecode(String &str) +{ + + const char *src = &str[0]; + uint8_t key[4]; + unsigned int enciphidx = 0; + unsigned int i; + uint8_t b; + uint32_t cid = ESP_getChipId(); + uint32_t *pkey = (uint32_t *)&key[0]; + uint8_t data[str.length()]; + unsigned int clen = base64_decode_chars(src, str.length(), (char *)data); + String result = F(""); + + if (0 != clen) { + *pkey = cid; + for (i = 0; i < clen; i++) { + b = data[i]; + b ^= key[enciphidx]; + ++enciphidx; + enciphidx %= sizeof(key); + result.concat(char(b)); + } + } + str = result; +} diff --git a/src/encString.hpp b/src/encString.hpp new file mode 100644 index 0000000..b794665 --- /dev/null +++ b/src/encString.hpp @@ -0,0 +1,9 @@ +#ifndef _ENCSTRING_HPP_ +#define _ENCSTRING_HPP_ +#include + +void strEncode(String &str); + +void strDecode(String &str); + +#endif