Skip to content

Add MD5 hash to HTTPUpdate http request headers #2165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
37 changes: 35 additions & 2 deletions cores/esp32/Esp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
extern "C" {
#include <esp_image_format.h>
}
#include <MD5Builder.h>

/**
* User-defined Literals
Expand Down Expand Up @@ -158,16 +159,48 @@ static uint32_t sketchSize(sketchSize_t response) {
data.start_addr = running_pos.offset;
esp_image_verify(ESP_IMAGE_VERIFY, &running_pos, &data);
if (response) {
return running_pos.size - data.image_len;
return running_pos.size - data.image_len;
} else {
return data.image_len;
return data.image_len;
}
}

uint32_t EspClass::getSketchSize () {
return sketchSize(SKETCH_SIZE_TOTAL);
}

String EspClass::getSketchMD5()
{
static String result;
if (result.length()) {
return result;
}
uint32_t lengthLeft = getSketchSize();

const esp_partition_t *running = esp_ota_get_running_partition();
if (!running) return String();
const size_t bufSize = SPI_FLASH_SEC_SIZE;
std::unique_ptr<uint8_t[]> buf(new uint8_t[bufSize]);
uint32_t offset = 0;
if(!buf.get()) {
return String();
}
MD5Builder md5;
md5.begin();
while( lengthLeft > 0) {
size_t readBytes = (lengthLeft < bufSize) ? lengthLeft : bufSize;
if (!ESP.flashRead(running->address + offset, reinterpret_cast<uint32_t*>(buf.get()), (readBytes + 3) & ~3)) {
return String();
}
md5.add(buf.get(), readBytes);
lengthLeft -= readBytes;
offset += readBytes;
}
md5.calculate();
result = md5.toString();
return result;
}

uint32_t EspClass::getFreeSketchSpace () {
const esp_partition_t* _partition = esp_ota_get_next_update_partition(NULL);
if(!_partition){
Expand Down
1 change: 1 addition & 0 deletions cores/esp32/Esp.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class EspClass
FlashMode_t magicFlashChipMode(uint8_t byte);

uint32_t getSketchSize();
String getSketchMD5();
uint32_t getFreeSketchSpace();

bool flashEraseSector(uint32_t sector);
Expand Down
4 changes: 2 additions & 2 deletions libraries/HTTPUpdate/src/HTTPUpdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren
http.addHeader("x-ESP32-AP-MAC", WiFi.softAPmacAddress());
http.addHeader("x-ESP32-free-space", String(ESP.getFreeSketchSpace()));
http.addHeader("x-ESP32-sketch-size", String(ESP.getSketchSize()));
// To do http.addHeader("x-ESP32-sketch-md5", String(ESP.getSketchMD5()));
// Sketch MD5 is not supported by the core, but SHA256 is, so add a SHA256 instead
http.addHeader("x-ESP32-sketch-md5", String(ESP.getSketchMD5()));
// Add also a SHA256
String sketchSHA256 = getSketchSHA256();
if(sketchSHA256.length() != 0) {
http.addHeader("x-ESP32-sketch-sha256", sketchSHA256);
Expand Down