#pragma once #include #include /** * \brief Remembers the last CLI replies sent, as a hash of each reply's plaintext (timestamp, flags, text), * so the same bytes coming back as a command (hash bytes swapped, same shared secret) are refused. */ class ReplyEchoGuard { static const int SLOTS = 16; uint32_t _hash[SLOTS] = {}; int _next = 0; static uint32_t hash(const uint8_t* p, size_t len) { // FNV-1a, never zero (zero marks an empty slot) uint32_t h = 2166136261u; for (size_t i = 0; i < len; i++) { h = (h ^ p[i]) * 16777619u; } return h ? h : 1; } public: void note(const uint8_t* plain, size_t len) { _hash[_next] = hash(plain, len); _next = (_next + 1) % SLOTS; } bool matches(const uint8_t* plain, size_t len) const { uint32_t h = hash(plain, len); for (int i = 0; i < SLOTS; i++) { if (_hash[i] == h) return true; } return false; } };