remove rweather/Crypt lib from firmware (save 2.1k of flash)

mbedtls is already integral part of the firmware. use it in favor of
rweather/Crypto library to calculate a sha256 checksum of a string, as
used in the HTTP power meter implementation.
This commit is contained in:
Bernhard Kirchen 2024-05-05 23:46:20 +02:00 committed by Bernhard Kirchen
parent 1dd64a57fd
commit b7a8bdf07b
2 changed files with 13 additions and 18 deletions

View File

@ -47,7 +47,6 @@ lib_deps =
https://github.com/coryjfowler/MCP_CAN_lib https://github.com/coryjfowler/MCP_CAN_lib
plerup/EspSoftwareSerial @ ^8.0.1 plerup/EspSoftwareSerial @ ^8.0.1
https://github.com/dok-net/ghostl @ ^1.0.1 https://github.com/dok-net/ghostl @ ^1.0.1
rweather/Crypto@^0.4.0
extra_scripts = extra_scripts =
pre:pio-scripts/auto_firmware_version.py pre:pio-scripts/auto_firmware_version.py

View File

@ -4,8 +4,7 @@
#include "MessageOutput.h" #include "MessageOutput.h"
#include <WiFiClientSecure.h> #include <WiFiClientSecure.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <Crypto.h> #include "mbedtls/sha256.h"
#include <SHA256.h>
#include <base64.h> #include <base64.h>
#include <memory> #include <memory>
#include <ESPmDNS.h> #include <ESPmDNS.h>
@ -324,27 +323,24 @@ bool HttpPowerMeterClass::extractUrlComponents(String url, String& _protocol, St
return true; return true;
} }
#define HASH_SIZE 32
String HttpPowerMeterClass::sha256(const String& data) { String HttpPowerMeterClass::sha256(const String& data) {
SHA256 sha256; uint8_t hash[32];
uint8_t hash[HASH_SIZE];
sha256.reset(); mbedtls_sha256_context ctx;
sha256.update(data.c_str(), data.length()); mbedtls_sha256_init(&ctx);
sha256.finalize(hash, HASH_SIZE); mbedtls_sha256_starts(&ctx, 0); // select SHA256
mbedtls_sha256_update(&ctx, reinterpret_cast<const unsigned char*>(data.c_str()), data.length());
mbedtls_sha256_finish(&ctx, hash);
mbedtls_sha256_free(&ctx);
String hashStr = ""; char res[sizeof(hash) * 2 + 1];
for (int i = 0; i < HASH_SIZE; i++) { for (int i = 0; i < sizeof(hash); i++) {
String hex = String(hash[i], HEX); snprintf(res + (i*2), sizeof(res) - (i*2), "%02x", hash[i]);
if (hex.length() == 1) {
hashStr += "0";
}
hashStr += hex;
} }
return hashStr; return res;
} }
void HttpPowerMeterClass::prepareRequest(uint32_t timeout, const char* httpHeader, const char* httpValue) { void HttpPowerMeterClass::prepareRequest(uint32_t timeout, const char* httpHeader, const char* httpValue) {
httpClient.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); httpClient.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
httpClient.setUserAgent("OpenDTU-OnBattery"); httpClient.setUserAgent("OpenDTU-OnBattery");