static PyObject *pysiphash(PyObject *self, PyObject *args) { const char *key = NULL; int key_sz; const char *plaintext = NULL; int plain_sz; uint64_t hash; if (!PyArg_ParseTuple( args, "s#s#:siphash", &key, &key_sz, &plaintext, &plain_sz)) { return NULL; } if (key_sz != 16) { PyErr_SetString( PyExc_ValueError, "key must be exactly 128 bits long (16 chars)"); return NULL; } hash = siphash( (const unsigned char*)key, (const unsigned char*)plaintext, plain_sz); return PyLong_FromUnsignedLongLong(hash); }
uint64_t ccv_cache_generate_signature(const char* msg, int len, uint64_t sig_start, ...) { uint64_t sig_out, sig_in[2]; // 1 is in, 0 is out siphash((uint8_t*)&sig_out, (const uint8_t*)msg, len, key_siphash); va_list arguments; va_start(arguments, sig_start); sig_in[0] = sig_out; sig_in[1] = sig_start; while (sig_in[1] != 0) { siphash((uint8_t*)&sig_out, (const uint8_t*)sig_in, sizeof(uint64_t) * 2, key_siphash); sig_in[0] = sig_out; sig_in[1] = va_arg(arguments, uint64_t); } va_end(arguments); return sig_out; }
int crypto_auth(unsigned char *out,const unsigned char *in,unsigned long long inlen,const unsigned char *k) { int i; union { u8 bytes[8]; u64 gpr; } hash; hash.gpr = siphash(k, in, inlen); for(i=0; i < 8; ++i) out[i] = hash.bytes[i]; return 0; }
static int lsiphash(lua_State *L) { uint8_t out[8]; uint8_t key[16]; size_t sz = 0; const char * str = luaL_checklstring(L, 1, &sz); uint8_t lower[sz]; int i; for (i=0;i<sz;i++) { lower[i] = tolower(str[i]); } size_t keysz = 0; const char * keystr = lua_tolstring(L, 2, &keysz); siphashsalt(key, keystr, keysz); siphash(out, lower, sz, key); lua_pushlstring(L, (const char *)out, 8); return 1; }
uint64_t w_str_hashl (const char *str, size_t len) { #ifdef W_CONF_SIPHASH static uint8_t key[16] = { 0, }; if (key[0] == 0) { /* Try getting data from /dev/urandom if possible */ int fd = open ("/dev/urandom", O_RDONLY); if (fd >= 0) { int ret = read (fd, key, 16); close (fd); if (ret < 16) fd = -1; } if (fd < 0) { struct timeval tv; gettimeofday (&tv, NULL); uint32_t *words = (uint32_t*) key; words[0] = tv.tv_sec ^ tv.tv_usec; words[1] = tv.tv_sec; words[2] = tv.tv_usec; words[3] = getpid (); } key[0] |= 0x01; } uint8_t hash[8] = { 0, }; siphash (hash, (const uint8_t*) str, len, key); return *((uint64_t*) hash); #else register uint64_t ret = 0; register uint64_t ctr = 0; w_assert(str != NULL); while (len-- && *str) { ret ^= *str++ << ctr; ctr = (ctr + 1) % sizeof (void*); } return ret; #endif /* W_CONF_SIPHASH */ }
/** * ostree_str_bloom_hash: * @element: element to calculate the hash for * @k: hash function index * * A universal hash function implementation for strings. It expects @element to * be a pointer to a string (i.e. @element has type `const gchar*`), and expects * @k to be in the range `[0, k_max)`, where `k_max` is the `k` value used to * construct the bloom filter. The output range from this hash function could be * any value in #guint64, and it handles input strings of any length. * * This function does not allow %NULL as a valid value for @element. * * Reference: * - https://www.131002.net/siphash/ * * Returns: hash of the string at @element using parameter @k * Since: 2017.8 */ guint64 ostree_str_bloom_hash (gconstpointer element, guint8 k) { const gchar *str = element; gsize str_len; union { guint64 u64; guint8 u8[8]; } out_le; guint8 k_array[16]; gsize i; str_len = strlen (str); for (i = 0; i < G_N_ELEMENTS (k_array); i++) k_array[i] = k; siphash ((const guint8 *) str, str_len, k_array, out_le.u8, sizeof (out_le)); return le64toh (out_le.u64); }
int test_vectors() { uint8_t in[MAXLEN], out[HASHLEN], k[KEYLEN]; int i; int fails = 0; for( i = 0; i < KEYLEN; ++i ) k[i] = i; for( i = 0; i < MAXLEN; ++i ) { in[i] = i; siphash( out, in, i, k ); if ( memcmp( out, vectors[i], HASHLEN ) ) { printf( "test vector failed for %d bytes\n", i ); fails++; } } return fails; }
static uint64_t mm_hash(uint64_t k) { return siphash(hash_key, (const uint8_t*)&k, 8); }
int main() { uint8_t in[64], out[16], k[16]; int i; int fails = 0; for (i = 0; i < 16; ++i) k[i] = i; for (int version = 0; version < 4; ++version) { #ifdef GETVECTORS printf("%s\n{\n", functions[version]); #else printf("%s\n", labels[version]); #endif for (i = 0; i < 64; ++i) { in[i] = i; int len = lengths[version]; if (version < 2) siphash(in, i, k, out, len); else halfsiphash(in, i, k, out, len); #ifdef GETVECTORS PRINTHASH(len); #else const uint8_t *v = NULL; switch (version) { case 0: v = (uint8_t *)vectors_sip64; break; case 1: v = (uint8_t *)vectors_sip128; break; case 2: v = (uint8_t *)vectors_hsip32; break; case 3: v = (uint8_t *)vectors_hsip64; break; default: break; } if (memcmp(out, v + (i * len), len)) { printf("fail for %d bytes\n", i); fails++; } #endif } #ifdef GETVECTORS printf("};\n"); #else if (!fails) printf("OK\n"); #endif fails = 0; } return 0; }
static inline uint64_t siphash_Block(const char* name, const size_t len) { uint64_t res; siphash(&res, (const uint8_t*)name, len, hashtable_key); return res; }
static void siphash_test (const void * key, int len, uint32_t seed, void * out) { uint64_t s[2]; s[0] = seed; s[1] = 0; siphash((uint8_t *)out, (const uint8_t *) key, len, (const uint8_t *) s); }