mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 15:12:06 +00:00 
			
		
		
		
	cleanup
This commit is contained in:
		| @@ -39,17 +39,6 @@ void MD5Digest::add(const uint8_t *data, size_t len) { br_md5_update(&this->ctx_ | ||||
| void MD5Digest::calculate() { br_md5_out(&this->ctx_, this->digest_); } | ||||
| #endif  // USE_RP2040 | ||||
|  | ||||
| void MD5Digest::get_bytes(uint8_t *output) { memcpy(output, this->digest_, 16); } | ||||
|  | ||||
| bool MD5Digest::equals_bytes(const uint8_t *expected) { | ||||
|   for (size_t i = 0; i < 16; i++) { | ||||
|     if (expected[i] != this->digest_[i]) { | ||||
|       return false; | ||||
|     } | ||||
|   } | ||||
|   return true; | ||||
| } | ||||
|  | ||||
| bool MD5Digest::equals_hex(const char *expected) { | ||||
|   uint8_t parsed[16]; | ||||
|   if (!parse_hex(expected, parsed, 16)) | ||||
|   | ||||
| @@ -42,16 +42,9 @@ class MD5Digest : public HashBase { | ||||
|   /// Compute the digest, based on the provided data. | ||||
|   void calculate() override; | ||||
|  | ||||
|   /// Retrieve the MD5 digest as bytes. | ||||
|   /// The output must be able to hold 16 bytes or more. | ||||
|   void get_bytes(uint8_t *output); | ||||
|  | ||||
|   /// Get the size of the hex output (32 for MD5) | ||||
|   size_t get_hex_size() const override { return 32; } | ||||
|  | ||||
|   /// Compare the digest against a provided byte-encoded digest (16 bytes). | ||||
|   bool equals_bytes(const uint8_t *expected); | ||||
|  | ||||
|   /// Compare the digest against a provided hex-encoded digest (32 bytes). | ||||
|   bool equals_hex(const char *expected); | ||||
|  | ||||
|   | ||||
| @@ -78,16 +78,12 @@ void SHA256::calculate() { | ||||
| #error "SHA256 not supported on this platform" | ||||
| #endif | ||||
|  | ||||
| void SHA256::get_bytes(uint8_t *output) { memcpy(output, this->digest_, 32); } | ||||
|  | ||||
| std::string SHA256::get_hex_string() { | ||||
|   char buf[65]; | ||||
|   this->get_hex(buf); | ||||
|   return std::string(buf); | ||||
| } | ||||
|  | ||||
| bool SHA256::equals_bytes(const uint8_t *expected) { return memcmp(this->digest_, expected, 32) == 0; } | ||||
|  | ||||
| bool SHA256::equals_hex(const char *expected) { | ||||
|   uint8_t parsed[32]; | ||||
|   if (!parse_hex(expected, parsed, 32)) { | ||||
|   | ||||
| @@ -33,13 +33,11 @@ class SHA256 : public esphome::HashBase { | ||||
|  | ||||
|   void calculate() override; | ||||
|  | ||||
|   void get_bytes(uint8_t *output); | ||||
|   std::string get_hex_string(); | ||||
|  | ||||
|   /// Get the size of the hex output (64 for SHA256) | ||||
|   size_t get_hex_size() const override { return 64; } | ||||
|  | ||||
|   bool equals_bytes(const uint8_t *expected); | ||||
|   bool equals_hex(const char *expected); | ||||
|  | ||||
|  protected: | ||||
|   | ||||
| @@ -2,6 +2,7 @@ | ||||
|  | ||||
| #include <cstdint> | ||||
| #include <cstddef> | ||||
| #include <cstring> | ||||
| #include "esphome/core/helpers.h" | ||||
|  | ||||
| namespace esphome { | ||||
| @@ -31,6 +32,18 @@ class HashBase { | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   /// 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); | ||||
|   } | ||||
|  | ||||
|   /// 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; | ||||
|   } | ||||
|  | ||||
|   /// Get the size of the hex output (32 for MD5, 64 for SHA256) | ||||
|   virtual size_t get_hex_size() const = 0; | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user