Exemple #1
0
        /// <summary>
        /// Hashes the byte buffer using the BuzHash algorithm
        /// </summary>
        /// <param name="buffer">byte byffer to hash</param>
        /// <param name="offset"> beginning of the window in the buffer</param>
        /// <param name="windowSize">window size in bytes</param>
        /// <returns>hash</returns>
        public uint Hash(byte[] buffer, int offset, int windowSize)
        {
            uint result = seed;

            for (var i = offset; i < offset + windowSize; i++)
            {
                result = result.BSL(1) ^ hashFunctionTable[buffer[i]];
            }
            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Hashes the byte buffer using the BuzHash algorithm
        /// </summary>
        /// <param name="stream">stream to hash</param>
        /// <param name="windowSize">window size in bytes</param>
        /// <returns>hash</returns>
        public uint Hash(Stream stream, int windowSize)
        {
            uint result = seed;

            for (int i = 0; i < windowSize && stream.Position < stream.Length; i++)
            {
                result = result.BSL(1) ^ hashFunctionTable[stream.ReadByte()];
            }
            return(result);
        }
Exemple #3
0
 /// <summary>
 /// This method is intended for use with sliding window hashing, and not for anything else. It allows to recalculate the hash
 /// when the window is sliden to the next byte
 /// </summary>
 /// <param name="oldHash">The hashcode of the byte before the window was sliden</param>
 /// <param name="newByte">New byte that was added to the window</param>
 /// <param name="oldByte">Old byte that was removed from the window</param>
 /// <param name="windowSize">Size of the window (in bytes)</param>
 /// <returns></returns>
 public uint UpdateHash(uint oldHash, byte newByte, byte oldByte, int windowSize)
 {
     return(oldHash.BSL(1) ^ hashFunctionTable[oldByte].BSL(windowSize) ^ hashFunctionTable[newByte]);
 }