Beispiel #1
0
        /// <inheritdoc />
        protected override byte[] ComputeHashInternal(IUnifiedData data, CancellationToken cancellationToken)
        {
            var prime  = _fnvPrimeOffset.Prime;
            var offset = _fnvPrimeOffset.Offset;

            // Handle 32-bit and 64-bit cases in a strongly-typed manner for performance
            if (_config.HashSizeInBits == 32)
            {
                var hash = offset[0];

                data.ForEachRead(
                    (dataBytes, position, length) => {
                    ProcessBytes32(ref hash, prime[0], dataBytes, position, length);
                },
                    cancellationToken);

                return(BitConverter.GetBytes(hash));
            }
            else if (_config.HashSizeInBits == 64)
            {
                var hash    = ((UInt64)offset[1] << 32) | offset[0];
                var prime64 = ((UInt64)prime[1] << 32) | prime[0];


                data.ForEachRead(
                    (dataBytes, position, length) => {
                    ProcessBytes64(ref hash, prime64, dataBytes, position, length);
                },
                    cancellationToken);

                return(BitConverter.GetBytes(hash));
            }


            // Process extended-sized FNV.
            {
                var hash = offset.ToArray();


                data.ForEachRead(
                    (dataBytes, position, length) => {
                    ProcessBytes(ref hash, prime, dataBytes, position, length);
                },
                    cancellationToken);

                return(UInt32ArrayToBytes(hash)
                       .ToArray());
            }
        }
        protected override byte[] ComputeHashInternal(IUnifiedData data, CancellationToken cancellationToken)
        {
            UInt32 h = 0;

            data.ForEachRead(
                (dataBytes, position, length) => {
                ProcessBytes(ref h, dataBytes, position, length);
            },
                cancellationToken);

            return(BitConverter.GetBytes(h));
        }
        /// <inheritdoc />
        protected override byte[] ComputeHashInternal(IUnifiedData data, CancellationToken cancellationToken)
        {
            var  h         = new byte[HashSizeInBits / 8];
            bool firstByte = true;

            data.ForEachRead(
                (dataBytes, position, length) => {
                ProcessBytes(ref h, ref firstByte, dataBytes, position, length);
            },
                cancellationToken);

            return(h);
        }
        /// <exception cref="System.InvalidOperationException">HashSize set to an invalid value.</exception>
        /// <inheritdoc />
        protected override byte[] ComputeHashInternal(IUnifiedData data, CancellationToken cancellationToken)
        {
            byte[] hash = null;

            switch (HashSizeInBits)
            {
            case 8:
            {
                byte h = (byte)_config.Seed;

                data.ForEachRead(
                    (dataBytes, position, length) =>
                    {
                        ProcessBytes(ref h, dataBytes, position, length);
                    },
                    cancellationToken);

                hash = new byte[] { h };
                break;
            }

            case 16:
            {
                UInt16 h = (UInt16)_config.Seed;

                data.ForEachRead(
                    (dataBytes, position, length) =>
                    {
                        ProcessBytes(ref h, dataBytes, position, length);
                    },
                    cancellationToken);

                hash = BitConverter.GetBytes(h);
                break;
            }

            case 32:
            {
                UInt32 h = (UInt32)_config.Seed;

                data.ForEachRead(
                    (dataBytes, position, length) =>
                    {
                        ProcessBytes(ref h, dataBytes, position, length);
                    },
                    cancellationToken);

                hash = BitConverter.GetBytes(h);
                break;
            }

            case 64:
            {
                UInt64 h = _config.Seed;

                data.ForEachRead(
                    (dataBytes, position, length) =>
                    {
                        ProcessBytes(ref h, dataBytes, position, length);
                    },
                    cancellationToken);

                hash = BitConverter.GetBytes(h);
                break;
            }

            default:
                throw new NotImplementedException();
            }

            return(hash);
        }