This repository has been archived on 2024-10-30. You can view files and clone it, but cannot push or open issues or pull requests.
esp-firewall/SourceCode/arduino/lib/Firewall/esp32Storage.cpp
Florian Hoss e5c061ff32 rename...
2022-04-20 14:37:21 +02:00

70 lines
1.7 KiB
C++

#include "esp32Storage.hpp"
namespace firewall
{
Storage::Storage(const uint16_t eeprom_size)
{
this->get_eeprom_hash();
this->eeprom_size = eeprom_size;
EEPROM.begin(this->eeprom_size);
this->amount_of_rules = EEPROM.read(this->settings_head);
log_i("Amount of Rules: %i", this->amount_of_rules);
}
Storage::~Storage()
{
}
void Storage::clear_eeprom()
{
for (int i = 0; i < this->eeprom_size; i++)
{
EEPROM.write(i, 0);
}
}
void Storage::get_eeprom_hash()
{
char buffer[this->eeprom_size] = {0};
for (int i = 0; i < this->eeprom_size; i++)
{
buffer[i] = EEPROM.readChar(i);
}
unsigned char *hashedPayload = get_hash(buffer);
for (int i = 0; i < sizeof(hashedPayload); i++)
{
char str[3];
sprintf(str, "%02x", (int)hashedPayload[i]);
Serial.print(str);
}
}
}
unsigned char *Storage::get_hash(const char *payload)
{
unsigned char hashedPayload[32];
mbedtls_md_context_t ctx;
mbedtls_md_type_t md_type = MBEDTLS_MD_MD5;
const size_t payloadLength = strlen(payload);
mbedtls_md_init(&ctx);
mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(md_type), 0);
mbedtls_md_starts(&ctx);
mbedtls_md_update(&ctx, (const unsigned char *)payload, payloadLength);
mbedtls_md_finish(&ctx, hashedPayload);
mbedtls_md_free(&ctx);
return hashedPayload;
}
uint8_t Storage::get_amount_of_firewall_rules()
{
return this->amount_of_rules;
}
void Storage::set_amount_of_firewall_rules(const uint8_t new_amount)
{
this->amount_of_rules = new_amount;
}
}