diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-07-04 16:46:24 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-07-04 16:46:24 -0400 |
| commit | 78960b1461a8fb184de4ffddf2d2ec4df729fb05 (patch) | |
| tree | f62f39c9d6b420c304bffba8143a41789980e68a | |
| parent | dfb7bb1984b555519851d084ac8b5f8e760c55ce (diff) | |
Randomize hash key on startup and rename to TOMO_HASH_KEY.
| -rw-r--r-- | builtins/array.c | 8 | ||||
| -rw-r--r-- | builtins/c_string.c | 2 | ||||
| -rw-r--r-- | builtins/functions.c | 12 | ||||
| -rw-r--r-- | builtins/functions.h | 2 | ||||
| -rw-r--r-- | builtins/pointer.c | 2 | ||||
| -rw-r--r-- | builtins/table.c | 2 | ||||
| -rw-r--r-- | builtins/text.c | 2 | ||||
| -rw-r--r-- | enums.c | 4 | ||||
| -rw-r--r-- | structs.c | 2 |
9 files changed, 20 insertions, 16 deletions
diff --git a/builtins/array.c b/builtins/array.c index 1b928635..69d5cff1 100644 --- a/builtins/array.c +++ b/builtins/array.c @@ -426,7 +426,7 @@ public uint32_t Array$hash(const array_t *arr, const TypeInfo *type) for (int64_t i = 0; i < arr->length; i++) { if (p >= end) { uint32_t chunk_hash; - halfsiphash(&hash_batch, sizeof(hash_batch), TOMO_HASH_VECTOR, (uint8_t*)&chunk_hash, sizeof(chunk_hash)); + halfsiphash(&hash_batch, sizeof(hash_batch), TOMO_HASH_KEY, (uint8_t*)&chunk_hash, sizeof(chunk_hash)); p = hash_batch; *(uint32_t*)p = chunk_hash; p += sizeof(uint32_t); @@ -434,7 +434,7 @@ public uint32_t Array$hash(const array_t *arr, const TypeInfo *type) memcpy((p += item_size), arr->data + i*arr->stride, item_size); } uint32_t hash; - halfsiphash(&hash_batch, ((int64_t)p) - ((int64_t)hash_batch), TOMO_HASH_VECTOR, (uint8_t*)&hash, sizeof(hash)); + halfsiphash(&hash_batch, ((int64_t)p) - ((int64_t)hash_batch), TOMO_HASH_KEY, (uint8_t*)&hash, sizeof(hash)); return hash; } else { uint32_t hash_batch[16] = {(uint32_t)arr->length}; @@ -442,14 +442,14 @@ public uint32_t Array$hash(const array_t *arr, const TypeInfo *type) for (int64_t i = 0; i < arr->length; i++) { if (p >= end) { uint64_t chunk_hash; - halfsiphash(&hash_batch, sizeof(hash_batch), TOMO_HASH_VECTOR, (uint8_t*)&chunk_hash, sizeof(chunk_hash)); + halfsiphash(&hash_batch, sizeof(hash_batch), TOMO_HASH_KEY, (uint8_t*)&chunk_hash, sizeof(chunk_hash)); p = hash_batch; *(p++) = chunk_hash; } *(p++) = generic_hash(arr->data + i*arr->stride, item); } uint32_t hash; - halfsiphash(&hash_batch, ((int64_t)p) - ((int64_t)hash_batch), TOMO_HASH_VECTOR, (uint8_t*)&hash, sizeof(hash)); + halfsiphash(&hash_batch, ((int64_t)p) - ((int64_t)hash_batch), TOMO_HASH_KEY, (uint8_t*)&hash, sizeof(hash)); return hash; } } diff --git a/builtins/c_string.c b/builtins/c_string.c index f74d6da2..3b258aad 100644 --- a/builtins/c_string.c +++ b/builtins/c_string.c @@ -38,7 +38,7 @@ public uint32_t CString$hash(const char **c_str) if (!*c_str) return 0; uint32_t hash; - halfsiphash(*c_str, strlen(*c_str), TOMO_HASH_VECTOR, (uint8_t*)&hash, sizeof(hash)); + halfsiphash(*c_str, strlen(*c_str), TOMO_HASH_KEY, (uint8_t*)&hash, sizeof(hash)); return hash; } diff --git a/builtins/functions.c b/builtins/functions.c index 7ece75ea..8916f936 100644 --- a/builtins/functions.c +++ b/builtins/functions.c @@ -7,6 +7,7 @@ #include <stdint.h> #include <stdlib.h> #include <sys/param.h> +#include <sys/random.h> #include <uninorm.h> #include <unistd.h> @@ -22,14 +23,17 @@ #include "types.h" #include "util.h" -public const char *TOMO_HASH_VECTOR = "tomo hash vector ---------------------------------------------"; +public uint8_t TOMO_HASH_KEY[8] = {0}; public void tomo_init(void) { GC_INIT(); USE_COLOR = getenv("COLOR") ? strcmp(getenv("COLOR"), "1") == 0 : isatty(STDOUT_FILENO); - srand(arc4random_uniform(UINT32_MAX)); - srand48(arc4random_uniform(UINT32_MAX)); + getrandom(TOMO_HASH_KEY, sizeof(TOMO_HASH_KEY), 0); + unsigned int seed; + getrandom(&seed, sizeof(seed), 0); + srand(seed); + srand48(seed); } public void fail(CORD fmt, ...) @@ -98,7 +102,7 @@ public uint32_t generic_hash(const void *obj, const TypeInfo *type) default: { hash_data:; uint32_t hash; - halfsiphash((void*)obj, type->size, TOMO_HASH_VECTOR, (uint8_t*)&hash, sizeof(hash)); + halfsiphash((void*)obj, type->size, TOMO_HASH_KEY, (uint8_t*)&hash, sizeof(hash)); return hash; } } diff --git a/builtins/functions.h b/builtins/functions.h index cf19a127..a62865bd 100644 --- a/builtins/functions.h +++ b/builtins/functions.h @@ -8,7 +8,7 @@ #include "types.h" -extern const char *TOMO_HASH_VECTOR; +extern uint8_t TOMO_HASH_KEY[8]; void tomo_init(void); diff --git a/builtins/pointer.c b/builtins/pointer.c index d71811b0..73bd41be 100644 --- a/builtins/pointer.c +++ b/builtins/pointer.c @@ -71,7 +71,7 @@ public bool Pointer$equal(const void *x, const void *y, const TypeInfo *type) { public uint32_t Pointer$hash(const void *x, const TypeInfo *type) { (void)type; uint32_t hash; - halfsiphash(x, sizeof(void*), TOMO_HASH_VECTOR, (uint8_t*)&hash, sizeof(hash)); + halfsiphash(x, sizeof(void*), TOMO_HASH_KEY, (uint8_t*)&hash, sizeof(hash)); return hash; } diff --git a/builtins/table.c b/builtins/table.c index 6b63be8d..6afca9ce 100644 --- a/builtins/table.c +++ b/builtins/table.c @@ -500,7 +500,7 @@ public uint32_t Table$hash(const table_t *t, const TypeInfo *type) default_hash, }; uint32_t hash; - halfsiphash(&components, sizeof(components), TOMO_HASH_VECTOR, (uint8_t*)&hash, sizeof(hash)); + halfsiphash(&components, sizeof(components), TOMO_HASH_KEY, (uint8_t*)&hash, sizeof(hash)); return hash; } diff --git a/builtins/text.c b/builtins/text.c index f39d42ee..666d5df0 100644 --- a/builtins/text.c +++ b/builtins/text.c @@ -123,7 +123,7 @@ public uint32_t Text$hash(const CORD *cord) uint8_t *normalized = _normalize(*cord, buf, &norm_len); uint32_t hash; - halfsiphash(normalized, norm_len, TOMO_HASH_VECTOR, (uint8_t*)&hash, sizeof(hash)); + halfsiphash(normalized, norm_len, TOMO_HASH_KEY, (uint8_t*)&hash, sizeof(hash)); if (normalized != buf) free(normalized); return hash; } @@ -124,7 +124,7 @@ static CORD compile_hash_method(env_t *env, ast_t *ast) return CORD_all("static uint32_t ", full_name, "$hash(const ", full_name, "_t *obj, const TypeInfo *info) {\n" "(void)info;\n" "uint32_t hash;\n" - "halfsiphash(&obj->$tag, sizeof(obj->$tag), TOMO_HASH_VECTOR, (uint8_t*)&hash, sizeof(hash));\n" + "halfsiphash(&obj->$tag, sizeof(obj->$tag), TOMO_HASH_KEY, (uint8_t*)&hash, sizeof(hash));\n" "return hash;" "\n}\n"); } @@ -144,7 +144,7 @@ static CORD compile_hash_method(env_t *env, ast_t *ast) } hash_func = CORD_all(hash_func, "}\n" "uint32_t hash;\n" - "halfsiphash(&hashes, sizeof(hashes), TOMO_HASH_VECTOR, (uint8_t*)&hash, sizeof(hash));\n" + "halfsiphash(&hashes, sizeof(hashes), TOMO_HASH_KEY, (uint8_t*)&hash, sizeof(hash));\n" "return hash;\n}\n"); return hash_func; } @@ -109,7 +109,7 @@ static CORD compile_hash_method(env_t *env, ast_t *ast) } hash_func = CORD_all(hash_func, "};\n" "uint32_t hash;\n" - "halfsiphash(&field_hashes, sizeof(field_hashes), TOMO_HASH_VECTOR, (uint8_t*)&hash, sizeof(hash));\n" + "halfsiphash(&field_hashes, sizeof(field_hashes), TOMO_HASH_KEY, (uint8_t*)&hash, sizeof(hash));\n" "return hash;\n}\n"); return hash_func; } |
