WiFi-Passwort now generated internally and device-specific

This commit is contained in:
2024-06-04 22:10:22 +02:00
parent 3d090dceb1
commit 25f05ed832
6 changed files with 112 additions and 10 deletions

View File

@@ -33,7 +33,7 @@ bool validateWiFiString(char *string, size_t size)
c == '.' || c == '/' || c == ':' || c == ';' || c == '<' ||
c == '=' || c == '>' || c == '?' || c == '@' || c == '[' ||
c == '\\' || c == ']' || c == '^' || c == '_' || c == '`' ||
c == '{' || c == '|' || c == '}' || c == '~'))
c == '{' || c == '|' || c == '}' || c == '~' || c == ' '))
{
// Found a character that is not a valid WiFi character.
return false;
@@ -78,7 +78,7 @@ void sanitizeWiFiString(const char *input, char *buffer, size_t bufferSize)
c == '.' || c == '/' || c == ':' || c == ';' || c == '<' ||
c == '=' || c == '>' || c == '?' || c == '@' || c == '[' ||
c == '\\' || c == ']' || c == '^' || c == '_' || c == '`' ||
c == '{' || c == '|' || c == '}' || c == '~'))
c == '{' || c == '|' || c == '}' || c == '~' || c == ' '))
{
// Replace invalid character with placeholder
buffer[i] = '_';
@@ -92,3 +92,34 @@ void sanitizeWiFiString(const char *input, char *buffer, size_t bufferSize)
// Null-terminate the buffer
buffer[i] = '\0';
}
/**
* @brief Generates a device-specific password based on a seed and the ESP8266 Chip ID.
*
* This function combines a given seed with the unique Chip ID of the ESP8266 device to create a device-specific password.
* The resulting password is stored in the provided character buffer.
*
* @param seed The seed string used for password generation. Should be up to 16 characters long.
* @param passwordBuffer The character buffer where the generated password will be stored.
* @param bufferLength The length of the password buffer. Should be large enough to hold the generated password and null terminator.
*/
void GenerateDeviceSpecificPassword(const char* seed, char* passwordBuffer, size_t bufferLength) {
uint32_t chipId = ESP.getChipId();
char chipIdStr[9]; // 8 characters + null terminator
snprintf(chipIdStr, sizeof(chipIdStr), "%08X", chipId);
char combined[33]; // seed (16) + chipId (8) + null terminator (1)
strncpy(combined, seed, 16);
strncat(combined, chipIdStr, 8);
// Simple hash function for demonstration
unsigned long hash = 5381;
int c;
char *str = combined;
while ((c = *str++)) {
hash = ((hash << 5) + hash) + c; // hash * 33 + c
}
snprintf(passwordBuffer, bufferLength, "%08X", hash);
}