|
19 | 19 | #include "ArduinoOTA.h" |
20 | 20 | #include "NetworkClient.h" |
21 | 21 | #include "ESPmDNS.h" |
| 22 | +#include "HEXBuilder.h" |
22 | 23 | #include "SHA2Builder.h" |
23 | 24 | #include "PBKDF2_HMACBuilder.h" |
24 | 25 | #include "Update.h" |
@@ -86,30 +87,29 @@ ArduinoOTAClass &ArduinoOTAClass::setPassword(const char *password) { |
86 | 87 |
|
87 | 88 | ArduinoOTAClass &ArduinoOTAClass::setPasswordHash(const char *password) { |
88 | 89 | if (_state == OTA_IDLE && password) { |
89 | | - // Store the pre-hashed password directly |
90 | | - _password.clear(); |
91 | | - _password = password; |
92 | | - |
93 | 90 | size_t len = strlen(password); |
| 91 | + bool is_hex = HEXBuilder::isHexString(password, len); |
94 | 92 |
|
95 | | - if (len == 32) { |
96 | | - // Check if it's a valid hex string (all chars are 0-9, a-f, A-F) |
97 | | - bool is_hex = true; |
98 | | - for (size_t i = 0; i < len; i++) { |
99 | | - char c = password[i]; |
100 | | - if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) { |
101 | | - is_hex = false; |
102 | | - break; |
103 | | - } |
104 | | - } |
| 93 | + if (!is_hex) { |
| 94 | + log_e("Invalid password hash. Expected hex string (0-9, a-f, A-F)."); |
| 95 | + return *this; |
| 96 | + } |
105 | 97 |
|
| 98 | + if (len == 32) { |
106 | 99 | // Warn if MD5 hash is detected (32 hex characters) |
107 | | - if (is_hex) { |
108 | | - log_w("MD5 password hash detected. MD5 is deprecated and insecure."); |
109 | | - log_w("Please use setPassword() with plain text or setPasswordHash() with SHA256 hash (64 chars)."); |
110 | | - log_w("To generate SHA256: echo -n 'yourpassword' | sha256sum"); |
111 | | - } |
| 100 | + log_w("MD5 password hash detected. MD5 is deprecated and insecure."); |
| 101 | + log_w("Please use setPassword() with plain text or setPasswordHash() with SHA256 hash (64 chars)."); |
| 102 | + log_w("To generate SHA256: echo -n 'yourpassword' | sha256sum"); |
| 103 | + } else if (len == 64) { |
| 104 | + log_i("Using SHA256 password hash."); |
| 105 | + } else { |
| 106 | + log_e("Invalid password hash length. Expected 32 (deprecated MD5) or 64 (SHA256) characters."); |
| 107 | + return *this; |
112 | 108 | } |
| 109 | + |
| 110 | + // Store the pre-hashed password directly |
| 111 | + _password.clear(); |
| 112 | + _password = password; |
113 | 113 | } |
114 | 114 | return *this; |
115 | 115 | } |
|
0 commit comments