/// <inheritdoc />
        protected override byte[] ComputeHashInternal(UnifiedData data)
        {
            UInt32 a = 0x9e3779b9;
            UInt32 b = 0x9e3779b9;
            UInt32 c = InitVal;

            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;
                });

            c += (UInt32) dataCount;

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


            return BitConverter.GetBytes(c);
        }
        protected override byte[] ComputeHashInternal(UnifiedData data)
        {
            if (HashSize != 0)
            {
                throw new InvalidOperationException("HashSize set to an invalid value.");
            }

            return(new byte[0]);
        }
        protected override async Task <byte[]> ComputeHashAsyncInternal(UnifiedData data)
        {
            if (HashSize != 0)
            {
                throw new InvalidOperationException("HashSize set to an invalid value.");
            }

            return(await Task.FromResult(new byte[0])
                   .ConfigureAwait(false));
        }
Example #4
0
        /// <inheritdoc />
        protected override async Task<byte[]> ComputeHashAsyncInternal(UnifiedData data)
        {
            UInt32 hash = 0;

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

            return BitConverter.GetBytes(hash);
        }
Example #5
0
        /// <inheritdoc />
        protected override byte[] ComputeHashInternal(UnifiedData data)
        {
            UInt32 hash = 0;

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

            return BitConverter.GetBytes(hash);
        }
        /// <inheritdoc />
        protected override byte[] ComputeHashInternal(UnifiedData data)
        {
            UInt32 h = 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);
                });
 
            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 async Task<byte[]> ComputeHashAsyncInternal(UnifiedData data)
        {
            byte[] hash = null;

            switch (HashSize)
            {
                case 32:
                {
                    UInt32 h1 = Seed;
                    int dataCount = 0;


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

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

                            dataCount += length;
                        }).ConfigureAwait(false);
            

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

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

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

                    int dataCount = 0;

            
                    await data.ForEachGroupAsync(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;
                        }).ConfigureAwait(false);


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

                    h1 += h2;
                    h2 += h1;

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

                    h1 += h2;
                    h2 += h1;


                    hash = new byte[16];

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

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

                    break;
                }
            }

            return hash;
        }
        /// <exception cref="System.InvalidOperationException">HashSize set to an invalid value.</exception>
        /// <inheritdoc />
        protected override async Task<byte[]> ComputeHashAsyncInternal(UnifiedData data)
        {
            UInt32 a = 0xdeadbeef + (UInt32) data.Length + InitVal1;
            UInt32 b = 0xdeadbeef + (UInt32) data.Length + InitVal1;
            UInt32 c = 0xdeadbeef + (UInt32) data.Length + InitVal1;

            if (HashSize == 64)
                c += InitVal2;


            int dataCount = 0;

            await data.ForEachGroupAsync(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);
                }).ConfigureAwait(false);
    
            if (dataCount > 0)
                Final(ref a, ref b, ref c);


            byte[] hash = null;

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

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

            return hash;
        }
 /// <summary>
 /// Computes hash value for given stream asynchronously.
 /// </summary>
 /// <param name="data">Data to hash.</param>
 /// <returns>
 /// Hash value of data as byte array.
 /// </returns>
 protected abstract Task<byte[]> ComputeHashAsyncInternal(UnifiedData data);
 /// <summary>
 /// Computes hash value for given stream asynchronously.
 /// </summary>
 /// <param name="data">Data to hash.</param>
 /// <returns>
 /// Hash value of data as byte array.
 /// </returns>
 protected abstract Task <byte[]> ComputeHashAsyncInternal(UnifiedData data);
        /// <exception cref="System.InvalidOperationException">HashSize set to an invalid value.</exception>
        /// <inheritdoc />
        protected override async Task<byte[]> ComputeHashAsyncInternal(UnifiedData data)
        {
            byte[] hash = null;

            switch (HashSize)
            {
                case 32:
                {        
                    const UInt32 m = unchecked((UInt32) MixConstant);

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

                    await data.ForEachGroupAsync(4, 
                        (dataGroup, position, length) => {
                            ProcessGroup(ref h, m, dataGroup, position, length);
                        }, 
                        (remainder, position, length) => {
                            ProcessRemainder(ref h, m, remainder, position, length);
                        }).ConfigureAwait(false);

                    // 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 = Seed ^ ((UInt64) data.Length * m);
            
                    await data.ForEachGroupAsync(8, 
                        (dataGroup, position, length) => {
                            ProcessGroup(ref h, m, dataGroup, position, length);
                        },
                        (remainder, position, length) => {
                            ProcessRemainder(ref h, m, remainder, position, length);
                        }).ConfigureAwait(false);
 
                    h ^= h >> 47;
                    h *= m;
                    h ^= h >> 47;

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

            return hash;
        }
Example #12
0
 protected override async Task <byte[]> ComputeHashAsyncInternal(UnifiedData data)
 {
     return(await Task.FromResult(new byte[0])
            .ConfigureAwait(false));
 }
Example #13
0
        /// <inheritdoc />
        protected override byte[] ComputeHashInternal(UnifiedData data)
        {
            // Use 64-bit variable regardless of CRC bit length
            UInt64 hash = Settings.InitialValue;

            // Reflect InitialValue if processing as big endian
            if (Settings.ReflectIn)
                hash = hash.ReflectBits(HashSize);


            // Store table reference in local variable to lower overhead.
            var crcTable = Settings.DataDivisionTable;


            // How much hash must be right-shifted to get the most significant byte (HashSize >= 8) or bit (HashSize < 8)
            int mostSignificantShift = HashSize - 8;

            if (HashSize < 8)
                mostSignificantShift = HashSize - 1;


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


            // Account for mixed-endianness
            if (Settings.ReflectIn ^ Settings.ReflectOut)
               hash = hash.ReflectBits(HashSize);


            hash ^= Settings.XOrOut;

            return hash.ToBytes(HashSize);
        }
 protected override byte[] ComputeHashInternal(UnifiedData data)
 {
     return new byte[0];
 }
Example #15
0
 /// <summary>
 /// Computes hash value for given stream.
 /// </summary>
 /// <param name="data">Data to hash.</param>
 /// <returns>
 /// Hash value of data as byte array.
 /// </returns>
 protected abstract byte[] ComputeHashInternal(UnifiedData data);
Example #16
0
		/// <inheritdoc />
		protected override async Task<byte[]> ComputeHashAsyncInternal(UnifiedData data)
		{
            var internalState = new InternalState(HashSize, _originalKeyLength, _salt, _personalization);


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

			await data.ForEachGroupAsync(
                BlockSizeBytes,
                (array, start, count) => ProcessBytes(internalState, array, start, count),
                (array, start, count) => ProcessBytes(internalState, array, start, count));

			return Final(HashSize, internalState);
		}
        /// <inheritdoc />
        protected override async Task<byte[]> ComputeHashAsyncInternal(UnifiedData data)
        {
            UInt32 h = Seed ^ ((UInt32) data.Length * m);

            await data.ForEachGroupAsync(4,
                (dataGroup, position, length) => {
                    ProcessGroup(ref h, dataGroup, position, length);
                },
                (remainder, position, length) => {
                    ProcessRemainder(ref h, remainder, position, length);
                }).ConfigureAwait(false);

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

            return BitConverter.GetBytes(h);
        }
 /// <summary>
 /// Computes hash value for given stream.
 /// </summary>
 /// <param name="data">Data to hash.</param>
 /// <returns>
 /// Hash value of data as byte array.
 /// </returns>
 protected abstract byte[] ComputeHashInternal(UnifiedData data);
 protected override async Task<byte[]> ComputeHashAsyncInternal(UnifiedData data)
 {
     return await Task.FromResult(new byte[0])
         .ConfigureAwait(false);
 }
Example #20
0
        /// <inheritdoc />
        protected override async Task<byte[]> ComputeHashAsyncInternal(UnifiedData data)
        {
            var prime = HashParameters[HashSize].Prime;
            var offset = HashParameters[HashSize].Offset;

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

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

                return BitConverter.GetBytes(hash);

            } else if (HashSize == 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);
                    }).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);
                    }).ConfigureAwait(false);

                return hash.ToBytes()
                    .ToArray();
            }
        }
Example #21
0
        /// <exception cref="System.InvalidOperationException">HashSize set to an invalid value.</exception>
        /// <inheritdoc />
        protected byte[] ComputeHashInternal(UnifiedData data)
        {
            byte[] hash = null;

            switch (_hashSize)
            {
            case 32:
            {
                var h = (uint)InitVal + Primes32[4];

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


                var initValues = new[]
                {
                    (uint)InitVal + Primes32[0] + Primes32[1],
                    (uint)InitVal + Primes32[1],
                    (uint)InitVal,
                    (uint)InitVal - Primes32[0]
                };

                data.ForEachGroup(16,
                                  (dataGroup, position, length) =>
                    {
                        for (var 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]  = RotateLeft32(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;
                    });


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

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

            case 64:
            {
                var h = InitVal + Primes64[4];

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

                var initValues = new[]
                {
                    InitVal + Primes64[0] + Primes64[1],
                    InitVal + Primes64[1],
                    InitVal,
                    InitVal - 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]  = RotateLeft64(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;
                    });


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

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

            return(hash);
        }
        /// <inheritdoc />
        protected override async Task<byte[]> ComputeHashAsyncInternal(UnifiedData data)
        {
            UInt32 a = 0x9e3779b9;
            UInt32 b = 0x9e3779b9;
            UInt32 c = InitVal;

            int dataCount = 0;

            await data.ForEachGroupAsync(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;
                }).ConfigureAwait(false);

            c += (UInt32) dataCount;

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


            return BitConverter.GetBytes(c);
        }
Example #23
0
        /// <exception cref="System.InvalidOperationException">HashSize set to an invalid value.</exception>
        /// <inheritdoc />
        protected override byte[] ComputeHashInternal(UnifiedData data)
        {
            byte[] hash = null;
            var dataArray = data.ToArray();
            
            switch (HashSize)
            {
                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;
            }

            return hash;
        }
Example #24
0
        /// <exception cref="System.InvalidOperationException">HashSize set to an invalid value.</exception>
        /// <inheritdoc />
        protected override async Task<byte[]> ComputeHashAsyncInternal(UnifiedData data)
        {
            byte[] hash = null;
            var dataArray = await data.ToArrayAsync()
                .ConfigureAwait(false);

            switch (HashSize)
            {
                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;
            }

            return hash;
        }
Example #25
0
        /// <exception cref="System.InvalidOperationException">HashSize set to an invalid value.</exception>
        /// <inheritdoc />
        protected override async Task<byte[]> ComputeHashAsyncInternal(UnifiedData data)
        {
            byte[] hash = null;
            
            switch (HashSize)
            {
                case 32:
                {
                    var h = ((UInt32) InitVal) + _primes32[4];

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


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

                    await data.ForEachGroupAsync(16, 
                        (dataGroup, position, length) => {
                            for (var 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] = initValues[y].RotateLeft(13);
                                    initValues[y] *= _primes32[0];
                                }
                            }

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

                            dataCount += length;
                        }).ConfigureAwait(false);

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

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

                case 64:
                {
                     var h = InitVal + _primes64[4];

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

                    var initValues = new[] {
                        InitVal + _primes64[0] + _primes64[1],
                        InitVal + _primes64[1],
                        InitVal,
                        InitVal - _primes64[0]
                    };


                    await data.ForEachGroupAsync(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] = initValues[y].RotateLeft(31);
                                    initValues[y] *= _primes64[0];
                                }
                            }

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

                            dataCount += remainder.Length;
                        }).ConfigureAwait(false);


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

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

            return hash;
        }
        /// <exception cref="System.InvalidOperationException">HashSize set to an invalid value.</exception>
        /// <inheritdoc />
        protected override async Task<byte[]> ComputeHashAsyncInternal(UnifiedData data)
        {
            UInt64[] h = new UInt64[12];
            
            h[0]=h[3]=h[6]=h[9]  = InitVal1;
            h[1]=h[4]=h[7]=h[10] = (HashSize == 128 ? InitVal2 : InitVal1);
            h[2]=h[5]=h[8]=h[11] = 0XDEADBEEFDEADBEEF;


            var remainderData = new byte[96];

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

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


            byte[] hash = null;

            switch (HashSize)
            {
                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;
            }

            return hash;
        }
Example #27
0
 protected override byte[] ComputeHashInternal(UnifiedData data)
 {
     return(new byte[0]);
 }