| 13 | } |
| 14 | |
| 15 | static Error hashFile(const std::filesystem::path& ipath, std::string& str) { // No ESP command |
| 16 | uint8_t shaResult[32]; |
| 17 | |
| 18 | try { |
| 19 | FileStream inFile { ipath.string(), "r" }; |
| 20 | uint8_t buf[512]; |
| 21 | size_t len; |
| 22 | |
| 23 | #ifdef WITH_MBEDTLS |
| 24 | mbedtls_md_context_t ctx; |
| 25 | |
| 26 | mbedtls_md_init(&ctx); |
| 27 | mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0); |
| 28 | mbedtls_md_starts(&ctx); |
| 29 | while ((len = inFile.read(buf, 512)) > 0) { |
| 30 | mbedtls_md_update(&ctx, buf, len); |
| 31 | feed_watchdog(); |
| 32 | } |
| 33 | mbedtls_md_finish(&ctx, shaResult); |
| 34 | mbedtls_md_free(&ctx); |
| 35 | #else |
| 36 | // Use the first 32 bytes of the file as a not-very-good "hash" |
| 37 | inFile.read(shaResult, 32); |
| 38 | #endif |
| 39 | } catch (const Error err) { |
| 40 | log_debug("Cannot hash file " << ipath.string()); |
| 41 | return Error::FsFailedOpenFile; |
| 42 | } |
| 43 | |
| 44 | str = '"'; |
| 45 | for (int i = 0; i < 32; i++) { |
| 46 | uint8_t b = shaResult[i]; |
| 47 | str += hexNibble(b >> 4); |
| 48 | str += hexNibble(b); |
| 49 | } |
| 50 | str += '"'; |
| 51 | |
| 52 | return Error::Ok; |
| 53 | } |
| 54 | |
| 55 | void HashFS::report_change() { |
| 56 | log_msg("Files changed"); |
no test coverage detected