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


            UInt64[] h = new UInt64[12];

            h[0] = h[3] = h[6] = h[9] = _seed1;
            h[1] = h[4] = h[7] = h[10] = _seed2;
            h[2] = h[5] = h[8] = h[11] = 0XDEADBEEFDEADBEEF;


            var remainderData = new byte[96];

            data.ForEachGroup(96,
                              (dataGroup, position, length) => {
                Mix(h, dataGroup, position, length);
            },
                              (remainder, position, length) => {
                Array.Copy(remainder, position, remainderData, 0, length);
                remainderData[95] = (byte)length;
            },
                              cancellationToken);

            Mix(h, remainderData, 0, remainderData.Length);
            End(h);


            byte[] hash;

            switch (_config.HashSizeInBits)
            {
            case 32:
                hash = BitConverter.GetBytes((UInt32)h[0]);
                break;

            case 64:
                hash = BitConverter.GetBytes(h[0]);
                break;

            case 128:
                hash = new byte[16];

                BitConverter.GetBytes(h[0])
                .CopyTo(hash, 0);

                BitConverter.GetBytes(h[1])
                .CopyTo(hash, 8);

                break;

            default:
                throw new NotImplementedException();
            }

            return(hash);
        }
        protected override byte[] ComputeHashInternal(IUnifiedData data, CancellationToken cancellationToken)
        {
            var dataArray = data.ToArray(cancellationToken);

            return(BitConverter.GetBytes(
                       ComputeHashFromArray(dataArray)));
        }
        /// <inheritdoc />
        protected override byte[] ComputeHashInternal(IUnifiedData data, CancellationToken cancellationToken)
        {
            UInt32 a = 0x9e3779b9;
            UInt32 b = 0x9e3779b9;
            UInt32 c = _config.Seed;

            int dataCount = 0;

            data.ForEachGroup(12,
                              (dataGroup, position, length) => {
                ProcessGroup(ref a, ref b, ref c, dataGroup, position, length);

                dataCount += length;
            },
                              (remainder, position, length) => {
                ProcessRemainder(ref a, ref b, ref c, remainder, position, length);

                dataCount += length;
            },
                              cancellationToken);

            c += (UInt32)dataCount;

            Mix(ref a, ref b, ref c);


            return(BitConverter.GetBytes(c));
        }
        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));
        }
        protected override byte[] ComputeHashInternal(IUnifiedData data, CancellationToken cancellationToken)
        {
            var internalState = new InternalState(_config.Seed);

            data.ForEachGroup(
                32,
                internalState.ProcessGroup,
                internalState.ProcessRemainder,
                cancellationToken);

            return(internalState.GetResult());
        }
        /// <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);
        }
Beispiel #7
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());
            }
        }
        /// <exception cref="System.InvalidOperationException">HashSize set to an invalid value.</exception>
        /// <inheritdoc />
        protected override byte[] ComputeHashInternal(IUnifiedData data, CancellationToken cancellationToken)
        {
            UInt32 a = 0xdeadbeef + (UInt32)data.Length + _config.Seed;
            UInt32 b = a;
            UInt32 c = a;

            if (_config.HashSizeInBits == 64)
            {
                c += _config.Seed2;
            }


            int dataCount = 0;

            data.ForEachGroup(12,
                              (dataGroup, position, length) => {
                ProcessGroup(ref a, ref b, ref c, ref dataCount, dataGroup, position, length);
            },
                              (remainder, position, length) => {
                ProcessRemainder(ref a, ref b, ref c, ref dataCount, remainder, position, length);
            },
                              cancellationToken);

            if (dataCount > 0)
            {
                Final(ref a, ref b, ref c);
            }


            byte[] hash;

            switch (_config.HashSizeInBits)
            {
            case 32:
                hash = BitConverter.GetBytes(c);
                break;

            case 64:
                hash = BitConverter.GetBytes((((UInt64)b) << 32) | c);
                break;

            default:
                throw new NotImplementedException();
            }

            return(hash);
        }
Beispiel #9
0
        /// <inheritdoc />
        protected override byte[] ComputeHashInternal(IUnifiedData data, CancellationToken cancellationToken)
        {
            var internalState = new InternalState(_config.HashSizeInBits, _originalKeyLength, _salt, _personalization);


            if (_originalKeyLength > 0)
            {
                ProcessBytes(internalState, _key, 0, _key.Length);
            }

            data.ForEachGroup(
                BlockSizeBytes,
                (array, start, count) => ProcessBytes(internalState, array, start, count),
                (array, start, count) => ProcessBytes(internalState, array, start, count),
                cancellationToken);

            return(Final(_config.HashSizeInBits, internalState));
        }
        /// <inheritdoc />
        protected override byte[] ComputeHashInternal(IUnifiedData data, CancellationToken cancellationToken)
        {
            UInt32 h = _config.Seed ^ ((UInt32)data.Length * m);

            data.ForEachGroup(4,
                              (dataGroup, position, length) => {
                ProcessGroup(ref h, dataGroup, position, length);
            },
                              (remainder, position, length) => {
                ProcessRemainder(ref h, remainder, position, length);
            },
                              cancellationToken);

            h *= m;
            h ^= h >> 10;
            h *= m;
            h ^= h >> 17;

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

            switch (_config.HashSizeInBits)
            {
            case 32:
                hash = BitConverter.GetBytes(
                    ComputeHash32(dataArray));

                break;

            case 64:
                hash = BitConverter.GetBytes(
                    ComputeHash64(dataArray));

                break;

            case 128:
                var result = ComputeHash128(dataArray);


                hash = new byte[16];

                BitConverter.GetBytes(result.Low)
                .CopyTo(hash, 0);

                BitConverter.GetBytes(result.High)
                .CopyTo(hash, 8);

                break;

            default:
                throw new NotImplementedException();
            }

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

            switch (_config.HashSizeInBits)
            {
            case 32:
            {
                const UInt32 m = unchecked ((UInt32)MixConstant);

                UInt32 h = (UInt32)_config.Seed ^ (UInt32)data.Length;

                data.ForEachGroup(4,
                                  (dataGroup, position, length) => {
                        ProcessGroup(ref h, m, dataGroup, position, length);
                    },
                                  (remainder, position, length) => {
                        ProcessRemainder(ref h, m, remainder, position, length);
                    },
                                  cancellationToken);

                // Do a few final mixes of the hash to ensure the last few
                // bytes are well-incorporated.

                h ^= h >> 13;
                h *= m;
                h ^= h >> 15;

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

            case 64:
            {
                const UInt64 m = MixConstant;

                UInt64 h = _config.Seed ^ ((UInt64)data.Length * m);

                data.ForEachGroup(8,
                                  (dataGroup, position, length) => {
                        ProcessGroup(ref h, m, dataGroup, position, length);
                    },
                                  (remainder, position, length) => {
                        ProcessRemainder(ref h, m, remainder, position, length);
                    },
                                  cancellationToken);

                h ^= h >> 47;
                h *= m;
                h ^= h >> 47;

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

            default:
            {
                throw new NotImplementedException();
            }
            }

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

            switch (_config.HashSizeInBits)
            {
            case 32:
            {
                var h = ((UInt32)_config.Seed) + _primes32[4];

                ulong  dataCount = 0;
                byte[] remainder = null;


                var initValues = new[] {
                    ((UInt32)_config.Seed) + _primes32[0] + _primes32[1],
                    ((UInt32)_config.Seed) + _primes32[1],
                    ((UInt32)_config.Seed),
                    ((UInt32)_config.Seed) - _primes32[0]
                };

                data.ForEachGroup(16,
                                  (dataGroup, position, length) => {
                        for (int x = position; x < position + length; x += 16)
                        {
                            for (var y = 0; y < 4; ++y)
                            {
                                initValues[y] += BitConverter.ToUInt32(dataGroup, x + (y * 4)) * _primes32[1];
                                initValues[y]  = RotateLeft(initValues[y], 13);
                                initValues[y] *= _primes32[0];
                            }
                        }

                        dataCount += (ulong)length;
                    },
                                  (remainderData, position, length) => {
                        remainder = new byte[length];
                        Array.Copy(remainderData, position, remainder, 0, length);

                        dataCount += (ulong)length;
                    },
                                  cancellationToken);


                PostProcess(ref h, initValues, dataCount, remainder);

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

            case 64:
            {
                var h = _config.Seed + _primes64[4];

                ulong  dataCount = 0;
                byte[] remainder = null;

                var initValues = new[] {
                    _config.Seed + _primes64[0] + _primes64[1],
                    _config.Seed + _primes64[1],
                    _config.Seed,
                    _config.Seed - _primes64[0]
                };


                data.ForEachGroup(32,
                                  (dataGroup, position, length) => {
                        for (var x = position; x < position + length; x += 32)
                        {
                            for (var y = 0; y < 4; ++y)
                            {
                                initValues[y] += BitConverter.ToUInt64(dataGroup, x + (y * 8)) * _primes64[1];
                                initValues[y]  = RotateLeft(initValues[y], 31);
                                initValues[y] *= _primes64[0];
                            }
                        }

                        dataCount += (ulong)length;
                    },
                                  (remainderData, position, length) => {
                        remainder = new byte[length];
                        Array.Copy(remainderData, position, remainder, 0, length);

                        dataCount += (ulong)length;
                    },
                                  cancellationToken);


                PostProcess(ref h, initValues, dataCount, remainder);

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

            default:
                throw new NotImplementedException();
            }

            return(hash);
        }
        private byte[] ComputeShortHashInternal(IUnifiedData data, CancellationToken cancellationToken)
        {
            var h = new UInt64[4] {
                _seed1,
                _seed2,
                0XDEADBEEFDEADBEEF,
                0XDEADBEEFDEADBEEF
            };

            var remainderData   = new byte[16];
            var remainderLength = 0;

            data.ForEachGroup(32,
                              (dataGroup, position, length) => {
                for (int x = position; x < position + length; x += 32)
                {
                    h[2] += BitConverter.ToUInt64(dataGroup, x);
                    h[3] += BitConverter.ToUInt64(dataGroup, x + 8);

                    ShortMix(h);

                    h[0] += BitConverter.ToUInt64(dataGroup, x + 16);
                    h[1] += BitConverter.ToUInt64(dataGroup, x + 24);
                }
            },
                              (remainder, position, length) => {
                if (length >= 16)
                {
                    h[2] += BitConverter.ToUInt64(remainder, position);
                    h[3] += BitConverter.ToUInt64(remainder, position + 8);

                    ShortMix(h);

                    position += 16;
                    length   -= 16;
                }

                if (length > 0)
                {
                    Array.Copy(remainder, position, remainderData, 0, length);
                    remainderLength = length;
                }
            },
                              cancellationToken);

            h[3] = ((UInt64)data.Length) << 56;


            if (remainderLength > 0)
            {
                h[3] += BitConverter.ToUInt64(remainderData, 8);
                h[2] += BitConverter.ToUInt64(remainderData, 0);
            }
            else
            {
                h[3] += 0XDEADBEEFDEADBEEF;
                h[2] += 0XDEADBEEFDEADBEEF;
            }

            ShortEnd(h);


            byte[] hash;

            switch (_config.HashSizeInBits)
            {
            case 32:
                hash = BitConverter.GetBytes((UInt32)h[0]);
                break;

            case 64:
                hash = BitConverter.GetBytes(h[0]);
                break;

            case 128:
                hash = new byte[16];

                BitConverter.GetBytes(h[0])
                .CopyTo(hash, 0);

                BitConverter.GetBytes(h[1])
                .CopyTo(hash, 8);

                break;

            default:
                throw new NotImplementedException();
            }

            return(hash);
        }
        /// <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);
        }
Beispiel #16
0
 protected override byte[] ComputeHashInternal(IUnifiedData data, CancellationToken cancellationToken)
 {
     return(OnComputeHashInternal(data, cancellationToken));
 }
Beispiel #17
0
 /// <summary>
 /// Computes hash value for given stream.
 /// </summary>
 /// <param name="data">Data to hash.</param>
 /// <param name="cancellationToken">A cancellation token to observe while calculating the hash value.</param>
 /// <returns>
 /// Hash value of data.
 /// </returns>
 /// <exception cref="TaskCanceledException">The <paramref name="cancellationToken"/> was canceled.</exception>
 protected abstract byte[] ComputeHashInternal(IUnifiedData data, CancellationToken cancellationToken);
Beispiel #18
0
        /// <exception cref="System.InvalidOperationException">HashSize set to an invalid value.</exception>
        /// <inheritdoc />
        protected override byte[] ComputeHashInternal(IUnifiedData data, CancellationToken cancellationToken)
        {
            byte[] hash;

            switch (_config.HashSizeInBits)
            {
            case 32:
            {
                UInt32 h1        = _config.Seed;
                int    dataCount = 0;


                data.ForEachGroup(4,
                                  (dataGroup, position, length) => {
                        ProcessGroup(ref h1, dataGroup, position, length);

                        dataCount += length;
                    },
                                  (remainder, position, length) => {
                        ProcessRemainder(ref h1, remainder, position, length);

                        dataCount += length;
                    },
                                  cancellationToken);


                h1 ^= (UInt32)dataCount;
                Mix(ref h1);

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

            case 128:
            {
                UInt64 h1 = (UInt64)_config.Seed;
                UInt64 h2 = (UInt64)_config.Seed;

                int dataCount = 0;


                data.ForEachGroup(16,
                                  (dataGroup, position, length) => {
                        ProcessGroup(ref h1, ref h2, dataGroup, position, length);

                        dataCount += length;
                    },
                                  (remainder, position, length) => {
                        ProcessRemainder(ref h1, ref h2, remainder, position, length);

                        dataCount += length;
                    },
                                  cancellationToken);


                h1 ^= (UInt64)dataCount;
                h2 ^= (UInt64)dataCount;

                h1 += h2;
                h2 += h1;

                Mix(ref h1);
                Mix(ref h2);

                h1 += h2;
                h2 += h1;


                var hashBytes = new byte[16];

                BitConverter.GetBytes(h1)
                .CopyTo(hashBytes, 0);

                BitConverter.GetBytes(h2)
                .CopyTo(hashBytes, 8);

                hash = hashBytes;
                break;
            }

            default:
                throw new NotImplementedException();
            }

            return(hash);
        }