feat: reporter — HMAC-signed hourly POST with 24-record offline buffer

Fix Arduino String .size() → .length() in hmac.cpp (pre-existing bug surfaced by compilation).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-14 06:28:24 -07:00
parent 6422e052df
commit 244426ec8b
3 changed files with 155 additions and 5 deletions

View File

@@ -15,8 +15,8 @@ static HString bytes_to_hex(const uint8_t* bytes, size_t len) {
}
static void hex_to_bytes(const HString& hex, uint8_t* out, size_t out_len) {
if (hex.size() % 2 != 0) return; // malformed — odd-length hex
for (size_t i = 0; i < out_len && (i * 2 + 1) < hex.size(); i++) {
if (hex.length() % 2 != 0) return; // malformed — odd-length hex
for (size_t i = 0; i < out_len && (i * 2 + 1) < hex.length(); i++) {
char byte_str[3] = {hex[i*2], hex[i*2+1], 0};
out[i] = (uint8_t)strtol(byte_str, nullptr, 16);
}
@@ -39,7 +39,7 @@ HString hmac_sign(const HString& secret_hex, const HString& device_id,
uint32_t timestamp, const HString& body) {
// 1. SHA256(body)
uint8_t body_hash[32] = {};
if (!sha256((const uint8_t*)body.c_str(), body.size(), body_hash)) {
if (!sha256((const uint8_t*)body.c_str(), body.length(), body_hash)) {
return HString{};
}
HString body_hash_hex = bytes_to_hex(body_hash, 32);
@@ -50,7 +50,7 @@ HString hmac_sign(const HString& secret_hex, const HString& device_id,
HString message = device_id + ":" + ts_buf + ":" + body_hash_hex;
// 3. Decode secret from hex
size_t secret_len = secret_hex.size() / 2;
size_t secret_len = secret_hex.length() / 2;
uint8_t secret[64] = {};
hex_to_bytes(secret_hex, secret, secret_len);
@@ -62,7 +62,7 @@ HString hmac_sign(const HString& secret_hex, const HString& device_id,
int ret2 = mbedtls_md_setup(&ctx, info, 1);
if (ret2 != 0) { mbedtls_md_free(&ctx); return HString{}; }
mbedtls_md_hmac_starts(&ctx, secret, secret_len);
mbedtls_md_hmac_update(&ctx, (const uint8_t*)message.c_str(), message.size());
mbedtls_md_hmac_update(&ctx, (const uint8_t*)message.c_str(), message.length());
mbedtls_md_hmac_finish(&ctx, hmac_result);
mbedtls_md_free(&ctx);