Пример #1
0
        /// <inheritdoc />
        protected override async Task <byte[]> ComputeHashAsyncInternal(IUnifiedDataAsync 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 (HashSizeInBits == 32)
            {
                var hash = offset[0];

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

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


                await data.ForEachReadAsync(
                    (dataBytes, position, length) => {
                    ProcessBytes64(ref hash, prime64, dataBytes, position, length);
                },
                    cancellationToken)
                .ConfigureAwait(false);

                return(BitConverter.GetBytes(hash));
            }


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


                await data.ForEachReadAsync(
                    (dataBytes, position, length) => {
                    ProcessBytes(ref hash, prime, dataBytes, position, length);
                },
                    cancellationToken)
                .ConfigureAwait(false);

                return(UInt32ArrayToBytes(hash)
                       .ToArray());
            }
        }
        protected override async Task <byte[]> ComputeHashAsyncInternal(IUnifiedDataAsync data, CancellationToken cancellationToken)
        {
            UInt32 h = 0;

            await data.ForEachReadAsync(
                (dataBytes, position, length) => {
                ProcessBytes(ref h, dataBytes, position, length);
            },
                cancellationToken)
            .ConfigureAwait(false);

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

            await data.ForEachReadAsync(
                (dataBytes, position, length) => {
                ProcessBytes(ref h, ref firstByte, dataBytes, position, length);
            },
                cancellationToken)
            .ConfigureAwait(false);

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

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

                await data.ForEachReadAsync(
                    (dataBytes, position, length) =>
                    {
                        ProcessBytes(ref h, dataBytes, position, length);
                    },
                    cancellationToken)
                .ConfigureAwait(false);

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

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

                await data.ForEachReadAsync(
                    (dataBytes, position, length) =>
                    {
                        ProcessBytes(ref h, dataBytes, position, length);
                    },
                    cancellationToken)
                .ConfigureAwait(false);

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

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

                await data.ForEachReadAsync(
                    (dataBytes, position, length) =>
                    {
                        ProcessBytes(ref h, dataBytes, position, length);
                    },
                    cancellationToken)
                .ConfigureAwait(false);

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

            case 64:
            {
                UInt64 h = _config.Seed;

                await data.ForEachReadAsync(
                    (dataBytes, position, length) =>
                    {
                        ProcessBytes(ref h, dataBytes, position, length);
                    },
                    cancellationToken)
                .ConfigureAwait(false);

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

            default:
                throw new NotImplementedException();
            }

            return(hash);
        }