Exemple #1
0
        public Dictionary <Hashs, byte[]> GetArray(IEnumerable <Hashs> hashNames, Stream stream)
        {
            if (stoping)
            {
                stoping = false;
            }
            if (hashNames.Count() != hashNames.Distinct().Count())
            {
                throw new ArgumentException("存在重复的哈希名");
            }
            var hashNameArray = hashNames.ToArray();

            HashAlgorithm[] hashes = hashNames.Select(p => p.Create()).ToArray();

            int hashCount = hashes.Length;

            byte[] buffer  = new byte[BufferLength];
            long[] offsets = new long[hashCount];

            long totalLength = stream.Length;
            int  length      = 0;

            while ((length = stream.Read(buffer, 0, BufferLength)) != 0)
            {
                if (stoping)
                {
                    foreach (var hash in hashes)
                    {
                        hash.Dispose();
                    }
                    HashAborted?.Invoke(this, new EventArgs());
                    return(null);
                }
                if (stream.Position < totalLength)
                {
                    Parallel.For(0, hashCount, i =>
                    {
                        offsets[i] += hashes[i].TransformBlock(buffer, 0, length, buffer, 0);
                    });
                }
                else
                {
                    Parallel.For(0, hashCount, i =>
                    {
                        hashes[i].TransformFinalBlock(buffer, 0, length);
                    });
                }
            }
            Dictionary <Hashs, byte[]> results = new Dictionary <Hashs, byte[]>();

            for (int i = 0; i < hashNameArray.Length; i++)
            {
                results.Add(hashNameArray[i], hashes[i].Hash);
            }
            foreach (var hash in hashes)
            {
                hash.Dispose();
            }
            return(results);
        }
Exemple #2
0
        public async Task <byte[][]> GetArrayAsync(IEnumerable <Hashs> hashNames, Stream stream)
        {
            if (stoping)
            {
                stoping = false;
            }
            HashAlgorithm[] hashes    = hashNames.Select(p => p.Create()).ToArray();
            int             hashCount = hashes.Length;

            byte[] buffer  = new byte[BufferLength];
            long[] offsets = new long[hashCount];

            long totalLength = stream.Length;
            int  length      = 0;

            byte[][] results = null;
            await Task.Run(() =>
            {
                while ((length = stream.Read(buffer, 0, BufferLength)) != 0)
                {
                    if (stoping)
                    {
                        foreach (var hash in hashes)
                        {
                            hash.Dispose();
                        }
                        HashAborted?.Invoke(this, new EventArgs());
                        return;
                    }
                    if (stream.Position < totalLength)
                    {
                        Parallel.For(0, hashCount, i =>
                        {
                            offsets[i] += hashes[i].TransformBlock(buffer, 0, length, buffer, 0);
                        });
                    }
                    else
                    {
                        Parallel.For(0, hashCount, i =>
                        {
                            hashes[i].TransformFinalBlock(buffer, 0, length);
                        });
                    }
                }
                results = hashes.Select(p => p.Hash).ToArray();
                foreach (var hash in hashes)
                {
                    hash.Dispose();
                }
            });

            return(results);
        }