From b41cc0226eabb2b3f66bc65255e0d3dff3cb1505 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 27 Jun 2025 00:24:45 +0200 Subject: [PATCH] Optimize OTA password storage from std::string to const char --- esphome/components/esphome/ota/ota_esphome.cpp | 7 ++++--- esphome/components/esphome/ota/ota_esphome.h | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/esphome/components/esphome/ota/ota_esphome.cpp b/esphome/components/esphome/ota/ota_esphome.cpp index 4cc82b9094..dca15f73ce 100644 --- a/esphome/components/esphome/ota/ota_esphome.cpp +++ b/esphome/components/esphome/ota/ota_esphome.cpp @@ -15,6 +15,7 @@ #include #include +#include namespace esphome { @@ -76,7 +77,7 @@ void ESPHomeOTAComponent::dump_config() { " Version: %d", network::get_use_address().c_str(), this->port_, USE_OTA_VERSION); #ifdef USE_OTA_PASSWORD - if (!this->password_.empty()) { + if (this->password_ != nullptr) { ESP_LOGCONFIG(TAG, " Password configured"); } #endif @@ -168,7 +169,7 @@ void ESPHomeOTAComponent::handle_() { this->writeall_(buf, 1); #ifdef USE_OTA_PASSWORD - if (!this->password_.empty()) { + if (this->password_ != nullptr) { buf[0] = ota::OTA_RESPONSE_REQUEST_AUTH; this->writeall_(buf, 1); md5::MD5Digest md5{}; @@ -187,7 +188,7 @@ void ESPHomeOTAComponent::handle_() { // prepare challenge md5.init(); - md5.add(this->password_.c_str(), this->password_.length()); + md5.add(this->password_, strlen(this->password_)); // add nonce md5.add(sbuf, 32); diff --git a/esphome/components/esphome/ota/ota_esphome.h b/esphome/components/esphome/ota/ota_esphome.h index e0d09ff37e..7ff3ac437a 100644 --- a/esphome/components/esphome/ota/ota_esphome.h +++ b/esphome/components/esphome/ota/ota_esphome.h @@ -13,7 +13,7 @@ namespace esphome { class ESPHomeOTAComponent : public ota::OTAComponent { public: #ifdef USE_OTA_PASSWORD - void set_auth_password(const std::string &password) { password_ = password; } + void set_auth_password(const char *password) { password_ = password; } #endif // USE_OTA_PASSWORD /// Manually set the port OTA should listen on @@ -32,7 +32,7 @@ class ESPHomeOTAComponent : public ota::OTAComponent { bool writeall_(const uint8_t *buf, size_t len); #ifdef USE_OTA_PASSWORD - std::string password_; + const char *password_{nullptr}; #endif // USE_OTA_PASSWORD uint16_t port_;