fix: ble_scanner sha256_prefix — guard mbedTLS null info and setup failure

This commit is contained in:
2026-04-14 06:26:20 -07:00
parent ccbbf689cf
commit 6422e052df

View File

@@ -20,15 +20,21 @@ static std::map<String, DeviceObs> s_seen;
static int s_max_concurrent = 0; static int s_max_concurrent = 0;
static String sha256_prefix(const String& input) { static String sha256_prefix(const String& input) {
uint8_t hash[32];
mbedtls_md_context_t ctx;
const mbedtls_md_info_t* info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256); const mbedtls_md_info_t* info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
if (!info) return String(); // SHA256 not available
uint8_t hash[32] = {};
mbedtls_md_context_t ctx;
mbedtls_md_init(&ctx); mbedtls_md_init(&ctx);
mbedtls_md_setup(&ctx, info, 0); if (mbedtls_md_setup(&ctx, info, 0) != 0) {
mbedtls_md_free(&ctx);
return String();
}
mbedtls_md_starts(&ctx); mbedtls_md_starts(&ctx);
mbedtls_md_update(&ctx, (const uint8_t*)input.c_str(), input.length()); mbedtls_md_update(&ctx, (const uint8_t*)input.c_str(), input.length());
mbedtls_md_finish(&ctx, hash); mbedtls_md_finish(&ctx, hash);
mbedtls_md_free(&ctx); mbedtls_md_free(&ctx);
String hex = ""; String hex = "";
char buf[3]; char buf[3];
for (int i = 0; i < 16; i++) { snprintf(buf, 3, "%02x", hash[i]); hex += buf; } for (int i = 0; i < 16; i++) { snprintf(buf, 3, "%02x", hash[i]); hex += buf; }