mtmd: use sha256 for input hashing (#27274)
* mtmd: use sha256 for input hashing * void conflict with boringssl
This commit is contained in:
Vendored
+16
-4
@@ -4,18 +4,30 @@ llama_add_compile_flags()
|
||||
|
||||
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||
|
||||
add_library(${TARGET} STATIC
|
||||
set(VENDOR_SRCS
|
||||
xxhash/xxhash.c
|
||||
sha1/sha1.c
|
||||
sha256/sha256.c
|
||||
)
|
||||
|
||||
# disable warnings in 3rd party code
|
||||
add_library(${TARGET} STATIC
|
||||
hash.cpp
|
||||
hash.h
|
||||
${VENDOR_SRCS}
|
||||
)
|
||||
|
||||
target_compile_features(${TARGET} PRIVATE cxx_std_17)
|
||||
|
||||
# disable warnings in 3rd party code, but keep them for hash.cpp
|
||||
if (CMAKE_C_COMPILER_ID STREQUAL "MSVC")
|
||||
target_compile_options(${TARGET} PRIVATE /w)
|
||||
set(NO_WARN_FLAG /w)
|
||||
else()
|
||||
target_compile_options(${TARGET} PRIVATE -w)
|
||||
set(NO_WARN_FLAG -w)
|
||||
endif()
|
||||
set_source_files_properties(${VENDOR_SRCS} PROPERTIES COMPILE_OPTIONS ${NO_WARN_FLAG})
|
||||
|
||||
# sha1 lives in a namespace to avoid a clash with boringssl, see scripts/sync_vendor.py
|
||||
set_source_files_properties(sha1/sha1.c PROPERTIES LANGUAGE CXX)
|
||||
|
||||
# sha256.c includes "rotate-bits/rotate-bits.h", so consumers get this dir too
|
||||
target_include_directories(${TARGET} PUBLIC .)
|
||||
|
||||
Vendored
+23
@@ -0,0 +1,23 @@
|
||||
#include "hash.h"
|
||||
|
||||
extern "C" {
|
||||
#include "sha256/sha256.h"
|
||||
}
|
||||
|
||||
static std::string to_hex(const unsigned char * digest, size_t len) {
|
||||
static const char hex[] = "0123456789abcdef";
|
||||
|
||||
std::string out;
|
||||
out.reserve(2*len);
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
out += hex[digest[i] >> 4];
|
||||
out += hex[digest[i] & 0xf];
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string hash_sha256_hex(const void * data, size_t len) {
|
||||
unsigned char digest[SHA256_DIGEST_SIZE];
|
||||
sha256_hash(digest, (const unsigned char *) data, len);
|
||||
return to_hex(digest, SHA256_DIGEST_SIZE);
|
||||
}
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
// C++ wrapper for the vendored hash functions
|
||||
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
|
||||
// returns the SHA-256 digest as a lowercase hex string
|
||||
std::string hash_sha256_hex(const void * data, size_t len);
|
||||
Vendored
+4
@@ -25,6 +25,8 @@ A million repetitions of "a"
|
||||
|
||||
#include "sha1.h"
|
||||
|
||||
namespace vendor_hash {
|
||||
|
||||
|
||||
#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
|
||||
|
||||
@@ -293,3 +295,5 @@ void SHA1(
|
||||
SHA1Final((unsigned char *)hash_out, &ctx);
|
||||
}
|
||||
|
||||
} // namespace vendor_hash
|
||||
|
||||
|
||||
Vendored
+2
-6
@@ -9,9 +9,7 @@
|
||||
|
||||
#include "stdint.h"
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
namespace vendor_hash {
|
||||
|
||||
typedef struct
|
||||
{
|
||||
@@ -45,8 +43,6 @@ void SHA1(
|
||||
const char *str,
|
||||
uint32_t len);
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
} // namespace vendor_hash
|
||||
|
||||
#endif /* SHA1_H */
|
||||
|
||||
Reference in New Issue
Block a user