/** * freebsdのDigest-FNV-1.00の移植 * License: Public Domain */ uint FNV_32_PRIME=0x01000193u;///32 bit magic FNV-0 and FNV-1 prime uint FNV0_32_INIT=0u; uint FNV1_32_INIT=0x811c9dc5u; private uint toFnv32(ubyte[] buf, uint hval){ //FNV-1 hash each octet in the buffer foreach(ubyte bp;buf){ // multiply by the 32 bit FNV magic prime mod 2^32 version(NO_FNV_GCC_OPTIMIZATION) hval *= FNV_32_PRIME; else hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24); hval ^= cast(uint)bp;//xor the bottom with the current octet } return hval;//return our new hash value } ///ラッパ class Fnv32{ ///fnv-0で初期化します。 this(){ this(0); } ///バージョンを指定して初期化します。 this(uint ver){ if(ver==0){ hval=FNV0_32_INIT; }else if(ver==1){ hval=FNV1_32_INIT; }else assert(0); } protected uint hval; ///計算します。 uint sum(ubyte[] buf){ hval=toFnv32(buf,hval); return hval; } } unittest{ Fnv32 fnv=new Fnv32(1); assert(fnv.sum(cast(ubyte[])"test")==3157003241); assert(fnv.sum(cast(ubyte[])"test")==341400645); } //int main(){return 0;}