1
0
mirror of https://github.com/janet-lang/janet synced 2025-10-22 03:07:41 +00:00

Allow disabling keyed hash function (prf) in conf

In some cases, one might want to disable what is currently
SipHash for speed / better security mechansims. For example, using
red black trees for caches rather than hash tables.
This commit is contained in:
Calvin Rose
2020-01-16 21:06:03 -06:00
parent 3d117804dd
commit 23c7c3bf1c
5 changed files with 20 additions and 1 deletions

View File

@@ -51,6 +51,7 @@
/* #define JANET_NO_PEG */
/* #define JANET_NO_TYPED_ARRAY */
/* #define JANET_NO_INT_TYPES */
/* #define JANET_NO_PRF */
/* #define JANET_OUT_OF_MEMORY do { printf("janet out of memory\n"); exit(1); } while (0) */
/* #define JANET_RECURSION_GUARD 1024 */
/* #define JANET_MAX_PROTO_DEPTH 200 */

View File

@@ -94,6 +94,18 @@ const char *const janet_status_names[16] = {
"alive"
};
#ifdef JANET_NO_PRF
int32_t janet_string_calchash(const uint8_t *str, int32_t len) {
const uint8_t *end = str + len;
uint32_t hash = 5381;
while (str < end)
hash = (hash << 5) + hash + *str++;
return (int32_t) hash;
}
#else
/*
Public domain siphash implementation sourced from:
@@ -202,6 +214,8 @@ int32_t janet_string_calchash(const uint8_t *str, int32_t len) {
return (int32_t)hash;
}
#endif
/* Computes hash of an array of values */
int32_t janet_array_calchash(const Janet *array, int32_t len) {
const Janet *end = array + len;

View File

@@ -1302,8 +1302,10 @@ JANET_API int janet_verify(JanetFuncDef *def);
JANET_API JanetBuffer *janet_pretty(JanetBuffer *buffer, int depth, int flags, Janet x);
/* Misc */
JANET_API int janet_equals(Janet x, Janet y);
#ifndef JANET_NO_PRF
JANET_API void janet_init_hash_key(uint8_t key[16]);
#endif
JANET_API int janet_equals(Janet x, Janet y);
JANET_API int32_t janet_hash(Janet x);
JANET_API int janet_compare(Janet x, Janet y);
JANET_API int janet_cstrcmp(JanetString str, const char *other);