mtmd: use sha256 for input hashing (#27274)

* mtmd: use sha256 for input hashing

* void conflict with boringssl
This commit is contained in:
Xuan-Son Nguyen
2026-08-17 21:20:12 +02:00
committed by GitHub
parent d8df12ebc4
commit ed1c3a20f5
12 changed files with 107 additions and 27 deletions
+1 -1
View File
@@ -78,7 +78,7 @@ set_target_properties(mtmd PROPERTIES
)
target_link_libraries (mtmd PUBLIC ggml llama)
target_link_libraries (mtmd PRIVATE Threads::Threads)
target_link_libraries (mtmd PRIVATE Threads::Threads vendor-hash)
target_include_directories(mtmd PUBLIC .)
target_include_directories(mtmd PRIVATE ../..)
target_include_directories(mtmd PRIVATE ../../vendor)
+4 -13
View File
@@ -12,6 +12,8 @@
#include "mtmd-helper-common.h"
#include "llama.h"
#include "hash.h"
#include <algorithm>
#include <cinttypes>
#include <vector>
@@ -356,25 +358,14 @@ static bool decode_audio_from_buf(const unsigned char * buf_in, size_t len, int
} // namespace audio_helpers
// Computes FNV-1a hash of the data
static std::string fnv_hash(const uint8_t * data, size_t len) {
const uint64_t fnv_prime = 0x100000001b3ULL;
uint64_t hash = 0xcbf29ce484222325ULL;
for (size_t i = 0; i < len; ++i) {
hash ^= data[i];
hash *= fnv_prime;
}
return std::to_string(hash);
}
mtmd_helper_bitmap_wrapper mtmd_helper_bitmap_init_from_buf(mtmd_context * ctx, const unsigned char * buf, size_t len, bool placeholder) {
// calculate the hash if needed
std::string id;
mtmd_bitmap * result = nullptr;
if (!placeholder) {
id = fnv_hash(buf, len);
// use sha256 to prevent cache poisoning
id = hash_sha256_hex(buf, len);
}
if (audio_helpers::is_audio_file((const char *)buf, len)) {
+1 -1
View File
@@ -49,7 +49,7 @@ MTMD_API struct mtmd_helper_bitmap_wrapper mtmd_helper_bitmap_init_from_file(mtm
// note:
// - for now, video input is only supported via C++ helper functions
// - audio files will be auto-detected based on magic bytes
// - output bitmap will have FNV hash as the ID
// - output bitmap will have SHA-256 hash (hex string) as the ID
// returns nullptr on failure
// this function is thread-safe
MTMD_API struct mtmd_helper_bitmap_wrapper mtmd_helper_bitmap_init_from_buf(mtmd_context * ctx, const unsigned char * buf, size_t len, bool placeholder);