//private static uint CalculateHash(byte[] buf, int len = -1, uint seed = 0) //{ // uint h32; // int index = 0; // if (len == -1) // { // len = buf.Length; // } // if (len >= 16) // { // int limit = len - 16; // uint v1 = seed + PRIME32_1 + PRIME32_2; // uint v2 = seed + PRIME32_2; // uint v3 = seed + 0; // uint v4 = seed - PRIME32_1; // do // { // v1 = CalcSubHash(v1, buf, index); // index += 4; // v2 = CalcSubHash(v2, buf, index); // index += 4; // v3 = CalcSubHash(v3, buf, index); // index += 4; // v4 = CalcSubHash(v4, buf, index); // index += 4; // } while (index <= limit); // h32 = RotateLeft(v1, 1) + RotateLeft(v2, 7) + RotateLeft(v3, 12) + RotateLeft(v4, 18); // } // else // { // h32 = seed + PRIME32_5; // } // h32 += (uint)len; // while (index <= len - 4) // { // h32 += BitConverter.ToUInt32(buf, index) * PRIME32_3; // h32 = RotateLeft(h32, 17) * PRIME32_4; // index += 4; // } // while (index < len) // { // h32 += buf[index] * PRIME32_5; // h32 = RotateLeft(h32, 11) * PRIME32_1; // index++; // } // h32 ^= h32 >> 15; // h32 *= PRIME32_2; // h32 ^= h32 >> 13; // h32 *= PRIME32_3; // h32 ^= h32 >> 16; // return h32; //} private static XxhState Init(XxhState state, uint seed = 0) { if (state.Memory != null) { return(state); } state.Seed = seed; state.V1 = seed + Prime321 + Prime322; state.V2 = seed + Prime322; state.V3 = seed + 0; state.V4 = seed - Prime321; state.TotalLen = 0; state.Memsize = 0; state.Memory = new byte[16]; return(state); }
private static uint Digest(XxhState state) { uint h32; var index = 0; if (state.TotalLen >= 16) { h32 = RotateLeft(state.V1, 1) + RotateLeft(state.V2, 7) + RotateLeft(state.V3, 12) + RotateLeft(state.V4, 18); } else { h32 = state.Seed + Prime325; } h32 += (UInt32)state.TotalLen; while (index <= state.Memsize - 4) { h32 += Peek4(state.Memory, index) * Prime323; h32 = RotateLeft(h32, 17) * Prime324; index += 4; } while (index < state.Memsize) { h32 += state.Memory[index] * Prime325; h32 = RotateLeft(h32, 11) * Prime321; index++; } h32 ^= h32 >> 15; h32 *= Prime322; h32 ^= h32 >> 13; h32 *= Prime323; h32 ^= h32 >> 16; return(h32); }
private static XxhState Update(byte[] input, int len, XxhState state) { int index = 0; state.TotalLen += (uint)len; if (state.Memsize + len < 16) { Array.Copy(input, 0, state.Memory, state.Memsize, len); state.Memsize += len; return(state); } if (state.Memsize > 0) { Array.Copy(input, 0, state.Memory, state.Memsize, 16 - state.Memsize); state.V1 = CalcSubHash(state.V1, state.Memory, index); index += 4; state.V2 = CalcSubHash(state.V2, state.Memory, index); index += 4; state.V3 = CalcSubHash(state.V3, state.Memory, index); index += 4; state.V4 = CalcSubHash(state.V4, state.Memory, index); //index += 4; index = 0; state.Memsize = 0; } if (index <= len - 16) { int limit = len - 16; uint v1 = state.V1; uint v2 = state.V2; uint v3 = state.V3; uint v4 = state.V4; do { v1 = CalcSubHash(v1, input, index); index += 4; v2 = CalcSubHash(v2, input, index); index += 4; v3 = CalcSubHash(v3, input, index); index += 4; v4 = CalcSubHash(v4, input, index); index += 4; } while (index <= limit); state.V1 = v1; state.V2 = v2; state.V3 = v3; state.V4 = v4; } if (index < len) { Array.Copy(input, index, state.Memory, 0, len - index); state.Memsize = len - index; } return(state); }