mirror of
https://github.com/ntop/n2n.git
synced 2024-09-19 16:41:11 +02:00
added 16-bit hashing
This commit is contained in:
parent
c7a8210078
commit
a64cfa450e
|
@ -19,3 +19,5 @@
|
|||
void pearson_hash_256 (uint8_t *out, const uint8_t *in, size_t len);
|
||||
|
||||
void pearson_hash_128 (uint8_t *out, const uint8_t *in, size_t len);
|
||||
|
||||
uint16_t pearson_hash_16 (const uint8_t *in, size_t len);
|
||||
|
|
|
@ -219,3 +219,23 @@ void pearson_hash_128 (uint8_t *out, const uint8_t *in, size_t len) {
|
|||
o = (uint64_t*)&out[8];
|
||||
*o = lower_hash;
|
||||
}
|
||||
|
||||
|
||||
// 16-bit hash: the return value has to be interpreted as uint16_t and
|
||||
// follows machine-specific endianess in memory
|
||||
uint16_t pearson_hash_16 (const uint8_t *in, size_t len) {
|
||||
|
||||
uint16_t hash = 0;
|
||||
uint16_t hash_mask = 0x0100;
|
||||
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
// broadcast the character, xor into hash, make them different permutations
|
||||
uint16_t c = (uint8_t)in[i];
|
||||
c |= c << 8;
|
||||
hash ^= c ^ hash_mask;
|
||||
// table lookup
|
||||
hash = t[(uint8_t)hash] + (t[hash >> 8] << 8);
|
||||
}
|
||||
// output
|
||||
return hash;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user