fx: add functions for hashing wstr and generic buffers

This commit is contained in:
2026-05-25 17:21:51 +01:00
parent 7cdad180da
commit 13332145c2
2 changed files with 34 additions and 5 deletions
+29 -2
View File
@@ -40,8 +40,8 @@ static const struct fx_hash_function_ops *hash_functions[] = {
[FX_HASH_SHAKE128] = &z__fx_shake128_ops, [FX_HASH_SHAKE128] = &z__fx_shake128_ops,
[FX_HASH_SHAKE256] = &z__fx_shake256_ops, [FX_HASH_SHAKE256] = &z__fx_shake256_ops,
}; };
static const size_t nr_hash_functions static const size_t nr_hash_functions = sizeof hash_functions
= sizeof hash_functions / sizeof hash_functions[0]; / sizeof hash_functions[0];
uint64_t fx_hash_cstr(const char *s) uint64_t fx_hash_cstr(const char *s)
{ {
@@ -66,6 +66,33 @@ uint64_t fx_hash_cstr_ex(const char *s, size_t *len)
return hash; return hash;
} }
uint64_t fx_hash_wstr(const fx_wchar *s)
{
uint64_t hash = FNV1_OFFSET_BASIS;
size_t i = 0;
for (i = 0; s[i]; i++) {
hash ^= s[i];
hash *= FNV1_PRIME;
}
return hash;
}
uint64_t fx_hash_buffer(const void *p, size_t len)
{
const char *s = p;
uint64_t hash = FNV1_OFFSET_BASIS;
size_t i = 0;
for (i = 0; i < len; i++) {
hash ^= s[i];
hash *= FNV1_PRIME;
}
return hash;
}
enum fx_status fx_hash_ctx_init( enum fx_status fx_hash_ctx_init(
struct fx_hash_ctx *ctx, struct fx_hash_ctx *ctx,
enum fx_hash_function func) enum fx_hash_function func)
+5 -3
View File
@@ -1,6 +1,7 @@
#ifndef FX_CORE_HASH_H_ #ifndef FX_CORE_HASH_H_
#define FX_CORE_HASH_H_ #define FX_CORE_HASH_H_
#include <fx/encoding.h>
#include <fx/misc.h> #include <fx/misc.h>
#include <fx/status.h> #include <fx/status.h>
#include <stddef.h> #include <stddef.h>
@@ -99,15 +100,16 @@ typedef struct fx_hash_ctx {
} fx_hash_ctx; } fx_hash_ctx;
FX_API uint64_t fx_hash_cstr(const char *s); FX_API uint64_t fx_hash_cstr(const char *s);
FX_API uint64_t fx_hash_wstr(const fx_wchar *s);
FX_API uint64_t fx_hash_cstr_ex(const char *s, size_t *len); FX_API uint64_t fx_hash_cstr_ex(const char *s, size_t *len);
FX_API uint64_t fx_hash_buffer(const void *s, size_t len);
FX_API fx_status fx_hash_ctx_init(fx_hash_ctx *ctx, fx_hash_function func); FX_API fx_status fx_hash_ctx_init(fx_hash_ctx *ctx, fx_hash_function func);
FX_API fx_status fx_hash_ctx_reset(fx_hash_ctx *ctx); FX_API fx_status fx_hash_ctx_reset(fx_hash_ctx *ctx);
FX_API fx_status FX_API fx_status
fx_hash_ctx_update(fx_hash_ctx *ctx, const void *p, size_t len); fx_hash_ctx_update(fx_hash_ctx *ctx, const void *p, size_t len);
FX_API fx_status fx_hash_ctx_update_rope( FX_API fx_status
fx_hash_ctx *ctx, fx_hash_ctx_update_rope(fx_hash_ctx *ctx, const struct fx_rope *rope);
const struct fx_rope *rope);
FX_API fx_status FX_API fx_status
fx_hash_ctx_finish(fx_hash_ctx *ctx, void *out_digest, size_t out_max); fx_hash_ctx_finish(fx_hash_ctx *ctx, void *out_digest, size_t out_max);