mirror of
https://github.com/esphome/esphome.git
synced 2025-09-26 15:12:21 +01:00
reduce magic numbers
This commit is contained in:
@@ -268,14 +268,14 @@ void ESPHomeOTAComponent::handle_data_() {
|
|||||||
// TODO: Remove this entire ifdef block in 2026.1.0
|
// TODO: Remove this entire ifdef block in 2026.1.0
|
||||||
if (client_supports_sha256) {
|
if (client_supports_sha256) {
|
||||||
sha256::SHA256 sha_hasher;
|
sha256::SHA256 sha_hasher;
|
||||||
auth_success = this->perform_hash_auth_(&sha_hasher, this->password_, 16, ota::OTA_RESPONSE_REQUEST_SHA256_AUTH,
|
auth_success = this->perform_hash_auth_(&sha_hasher, this->password_, ota::OTA_RESPONSE_REQUEST_SHA256_AUTH,
|
||||||
LOG_STR("SHA256"), sbuf);
|
LOG_STR("SHA256"), sbuf);
|
||||||
} else {
|
} else {
|
||||||
#ifdef USE_OTA_MD5
|
#ifdef USE_OTA_MD5
|
||||||
ESP_LOGW(TAG, "Using MD5 auth for compatibility (deprecated)");
|
ESP_LOGW(TAG, "Using MD5 auth for compatibility (deprecated)");
|
||||||
md5::MD5Digest md5_hasher;
|
md5::MD5Digest md5_hasher;
|
||||||
auth_success = this->perform_hash_auth_(&md5_hasher, this->password_, 8, ota::OTA_RESPONSE_REQUEST_AUTH,
|
auth_success =
|
||||||
LOG_STR("MD5"), sbuf);
|
this->perform_hash_auth_(&md5_hasher, this->password_, ota::OTA_RESPONSE_REQUEST_AUTH, LOG_STR("MD5"), sbuf);
|
||||||
#endif // USE_OTA_MD5
|
#endif // USE_OTA_MD5
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
@@ -286,7 +286,7 @@ void ESPHomeOTAComponent::handle_data_() {
|
|||||||
goto error; // NOLINT(cppcoreguidelines-avoid-goto)
|
goto error; // NOLINT(cppcoreguidelines-avoid-goto)
|
||||||
}
|
}
|
||||||
sha256::SHA256 sha_hasher;
|
sha256::SHA256 sha_hasher;
|
||||||
auth_success = this->perform_hash_auth_(&sha_hasher, this->password_, 16, ota::OTA_RESPONSE_REQUEST_SHA256_AUTH,
|
auth_success = this->perform_hash_auth_(&sha_hasher, this->password_, ota::OTA_RESPONSE_REQUEST_SHA256_AUTH,
|
||||||
LOG_STR("SHA256"), sbuf);
|
LOG_STR("SHA256"), sbuf);
|
||||||
#endif // ALLOW_OTA_DOWNGRADE_MD5
|
#endif // ALLOW_OTA_DOWNGRADE_MD5
|
||||||
#else
|
#else
|
||||||
@@ -295,7 +295,7 @@ void ESPHomeOTAComponent::handle_data_() {
|
|||||||
#ifdef USE_OTA_MD5
|
#ifdef USE_OTA_MD5
|
||||||
md5::MD5Digest md5_hasher;
|
md5::MD5Digest md5_hasher;
|
||||||
auth_success =
|
auth_success =
|
||||||
this->perform_hash_auth_(&md5_hasher, this->password_, 8, ota::OTA_RESPONSE_REQUEST_AUTH, LOG_STR("MD5"), sbuf);
|
this->perform_hash_auth_(&md5_hasher, this->password_, ota::OTA_RESPONSE_REQUEST_AUTH, LOG_STR("MD5"), sbuf);
|
||||||
#endif // USE_OTA_MD5
|
#endif // USE_OTA_MD5
|
||||||
#endif // USE_OTA_SHA256
|
#endif // USE_OTA_SHA256
|
||||||
|
|
||||||
@@ -529,10 +529,11 @@ void ESPHomeOTAComponent::log_auth_warning_(const LogString *action, const LogSt
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Non-template function definition to reduce binary size
|
// Non-template function definition to reduce binary size
|
||||||
bool ESPHomeOTAComponent::perform_hash_auth_(HashBase *hasher, const std::string &password, size_t nonce_size,
|
bool ESPHomeOTAComponent::perform_hash_auth_(HashBase *hasher, const std::string &password, uint8_t auth_request,
|
||||||
uint8_t auth_request, const LogString *name, char *buf) {
|
const LogString *name, char *buf) {
|
||||||
// Get sizes from the hasher
|
// Get sizes from the hasher
|
||||||
const size_t hex_size = hasher->get_size() * 2; // Hex is twice the byte size
|
const size_t hex_size = hasher->get_size() * 2; // Hex is twice the byte size
|
||||||
|
const size_t nonce_hex_len = hasher->get_size() / 2; // Nonce hex length is 1/4 of full hex size
|
||||||
|
|
||||||
// Use the provided buffer for all hex operations
|
// Use the provided buffer for all hex operations
|
||||||
|
|
||||||
@@ -552,7 +553,7 @@ bool ESPHomeOTAComponent::perform_hash_auth_(HashBase *hasher, const std::string
|
|||||||
nonce_bytes[2] = (r1 >> 8) & 0xFF;
|
nonce_bytes[2] = (r1 >> 8) & 0xFF;
|
||||||
nonce_bytes[3] = r1 & 0xFF;
|
nonce_bytes[3] = r1 & 0xFF;
|
||||||
|
|
||||||
if (nonce_size == 8) {
|
if (nonce_hex_len == 8) {
|
||||||
// MD5: 8 chars = "%08x" format = 4 bytes from one random uint32
|
// MD5: 8 chars = "%08x" format = 4 bytes from one random uint32
|
||||||
hasher->add(nonce_bytes, 4);
|
hasher->add(nonce_bytes, 4);
|
||||||
} else {
|
} else {
|
||||||
|
@@ -32,8 +32,8 @@ class ESPHomeOTAComponent : public ota::OTAComponent {
|
|||||||
void handle_handshake_();
|
void handle_handshake_();
|
||||||
void handle_data_();
|
void handle_data_();
|
||||||
#ifdef USE_OTA_PASSWORD
|
#ifdef USE_OTA_PASSWORD
|
||||||
bool perform_hash_auth_(HashBase *hasher, const std::string &password, size_t nonce_size, uint8_t auth_request,
|
bool perform_hash_auth_(HashBase *hasher, const std::string &password, uint8_t auth_request, const LogString *name,
|
||||||
const LogString *name, char *buf);
|
char *buf);
|
||||||
void log_auth_warning_(const LogString *action, const LogString *hash_name);
|
void log_auth_warning_(const LogString *action, const LogString *hash_name);
|
||||||
#endif // USE_OTA_PASSWORD
|
#endif // USE_OTA_PASSWORD
|
||||||
bool readall_(uint8_t *buf, size_t len);
|
bool readall_(uint8_t *buf, size_t len);
|
||||||
|
@@ -166,7 +166,7 @@ def check_error(data: list[int] | bytes, expect: int | list[int] | None) -> None
|
|||||||
raise OTAError("Error: Authentication invalid. Is the password correct?")
|
raise OTAError("Error: Authentication invalid. Is the password correct?")
|
||||||
if dat == RESPONSE_ERROR_WRITING_FLASH:
|
if dat == RESPONSE_ERROR_WRITING_FLASH:
|
||||||
raise OTAError(
|
raise OTAError(
|
||||||
"Error: Wring OTA data to flash memory failed. See USB logs for more "
|
"Error: Writing OTA data to flash memory failed. See USB logs for more "
|
||||||
"information."
|
"information."
|
||||||
)
|
)
|
||||||
if dat == RESPONSE_ERROR_UPDATE_END:
|
if dat == RESPONSE_ERROR_UPDATE_END:
|
||||||
|
Reference in New Issue
Block a user