mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	Merge branch 'sha256_ota' into integration
This commit is contained in:
		| @@ -532,7 +532,7 @@ void ESPHomeOTAComponent::log_auth_warning_(const LogString *action, const LogSt | ||||
| bool ESPHomeOTAComponent::perform_hash_auth_(HashBase *hasher, const std::string &password, size_t nonce_size, | ||||
|                                              uint8_t auth_request, const LogString *name, char *buf) { | ||||
|   // Get sizes from the hasher | ||||
|   const size_t hex_size = hasher->get_hex_size(); | ||||
|   const size_t hex_size = hasher->get_size() * 2;  // Hex is twice the byte size | ||||
|  | ||||
|   // Use the provided buffer for all hex operations | ||||
|  | ||||
|   | ||||
| @@ -42,8 +42,8 @@ class MD5Digest : public HashBase { | ||||
|   /// Compute the digest, based on the provided data. | ||||
|   void calculate() override; | ||||
|  | ||||
|   /// Get the size of the hex output (32 for MD5) | ||||
|   size_t get_hex_size() const override { return 32; } | ||||
|   /// Get the size of the hash in bytes (16 for MD5) | ||||
|   size_t get_size() const override { return 16; } | ||||
|  | ||||
|  protected: | ||||
|   MD5_CTX_TYPE ctx_{}; | ||||
|   | ||||
| @@ -33,8 +33,8 @@ class SHA256 : public esphome::HashBase { | ||||
|  | ||||
|   void calculate() override; | ||||
|  | ||||
|   /// Get the size of the hex output (64 for SHA256) | ||||
|   size_t get_hex_size() const override { return 64; } | ||||
|   /// Get the size of the hash in bytes (32 for SHA256) | ||||
|   size_t get_size() const override { return 32; } | ||||
|  | ||||
|  protected: | ||||
| #if defined(USE_ESP32) || defined(USE_LIBRETINY) | ||||
|   | ||||
| @@ -23,15 +23,11 @@ class HashBase { | ||||
|   virtual void calculate() = 0; | ||||
|  | ||||
|   /// Retrieve the hash as bytes | ||||
|   void get_bytes(uint8_t *output) { | ||||
|     const size_t hash_bytes = this->get_hex_size() / 2; | ||||
|     memcpy(output, this->digest_, hash_bytes); | ||||
|   } | ||||
|   void get_bytes(uint8_t *output) { memcpy(output, this->digest_, this->get_size()); } | ||||
|  | ||||
|   /// Retrieve the hash as hex characters | ||||
|   void get_hex(char *output) { | ||||
|     const size_t hash_bytes = this->get_hex_size() / 2; | ||||
|     for (size_t i = 0; i < hash_bytes; i++) { | ||||
|     for (size_t i = 0; i < this->get_size(); i++) { | ||||
|       uint8_t byte = this->digest_[i]; | ||||
|       output[i * 2] = format_hex_char(byte >> 4); | ||||
|       output[i * 2 + 1] = format_hex_char(byte & 0x0F); | ||||
| @@ -39,23 +35,19 @@ class HashBase { | ||||
|   } | ||||
|  | ||||
|   /// Compare the hash against a provided byte-encoded hash | ||||
|   bool equals_bytes(const uint8_t *expected) { | ||||
|     const size_t hash_bytes = this->get_hex_size() / 2; | ||||
|     return memcmp(this->digest_, expected, hash_bytes) == 0; | ||||
|   } | ||||
|   bool equals_bytes(const uint8_t *expected) { return memcmp(this->digest_, expected, this->get_size()) == 0; } | ||||
|  | ||||
|   /// Compare the hash against a provided hex-encoded hash | ||||
|   bool equals_hex(const char *expected) { | ||||
|     const size_t hash_bytes = this->get_hex_size() / 2; | ||||
|     uint8_t parsed[32];  // Max size for SHA256 | ||||
|     if (!parse_hex(expected, parsed, hash_bytes)) { | ||||
|     if (!parse_hex(expected, parsed, this->get_size())) { | ||||
|       return false; | ||||
|     } | ||||
|     return this->equals_bytes(parsed); | ||||
|   } | ||||
|  | ||||
|   /// Get the size of the hex output (32 for MD5, 64 for SHA256) | ||||
|   virtual size_t get_hex_size() const = 0; | ||||
|   /// Get the size of the hash in bytes (16 for MD5, 32 for SHA256) | ||||
|   virtual size_t get_size() const = 0; | ||||
|  | ||||
|  protected: | ||||
|   uint8_t digest_[32];  // Common digest storage, sized for largest hash (SHA256) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user