#define V3HTEST /* v3bhash. Suitable for fast non-cryptographic hashing. http://cipherdev.org/ 2011-03-16: updated mixing core. fixed padding again. added padding test :) 2010-10-26: the final block wasn't handled right if it was 10 or 11 bytes. oops. 2009-05-18: initial release This file is released into the public domain through CC0. No rights reserved. Legal stuff: http://creativecommons.org/publicdomain/zero/1.0/ */ #include #include #include #define rot(a,b) (((a)<<(b))|((a)>>(32-(b)))) #define MIX(a, b, c, d) \ a=rot(a+d,21); b=rot(b,12)+c; c=c^a; d=d^b; \ a=rot(a+d,19); b=rot(b,24)+c; c=c^a; d=d^b; \ a=rot(a+d, 7); b=rot(b,12)+c; c=c^a; d=d^b; \ a=rot(a+d,27); b=rot(b,17)+c; c=c^a; d=d^b; uint32_t v3bhash(uint8_t *in, unsigned int len, uint32_t initval) { unsigned int blocks = len/16, remainder = len%16; register uint32_t m[4] = {len, initval, 11111111, 22222222}; uint32_t *in32 = (uint32_t*)in, *fpos = &in32[blocks*4]; // Mix initialization data (len, initval). MIX(m[0], m[1], m[2], m[3]); // Go through the input in 16-byte blocks. for(;in32=i)) { printf("byte %u not handled right at length %u\n", j, i); fail = 1; } data[j] ^= 1; } } printf("v3bhash padding test %s\n", fail ? "FAILED" : "ok"); return fail; } #endif