diff --git a/builtins/array.c b/builtins/array.c index 1b92863..69d5cff 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 f74d6da..3b258aa 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 7ece75e..8916f93 100644 --- a/builtins/functions.c +++ b/builtins/functions.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -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 cf19a12..a62865b 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 d71811b..73bd41b 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 6b63be8..6afca9c 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 f39d42e..666d5df 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; } diff --git a/enums.c b/enums.c index 344b5f1..4e63221 100644 --- a/enums.c +++ b/enums.c @@ -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; } diff --git a/structs.c b/structs.c index 51f6928..22965b7 100644 --- a/structs.c +++ b/structs.c @@ -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; }