Esempio n. 1
0
        byte[] CalculateHash(Stream stream, bool create)
        {
            long pos = stream.Position;

            if (create)
            {
                _bodyLength = StreamLength(stream);
            }

            using (HashAlgorithm hasher = (_snk != null) ? _snk.CreateHasher() : QQnCryptoHelpers.CreateHashAlgorithm(_hashType))
            {
                long newPos = _hashPosition;

                if (stream.Position != newPos)
                {
                    stream.Position = newPos;
                }

                byte[] buffer;

                using (MemoryStream ms = new MemoryStream())                 // Use memorystream and writer to resolve endian-issues
                    using (QQnBinaryWriter bw = new QQnBinaryWriter(ms))
                    {
                        bw.Write(_guid.ToByteArray());
                        bw.Write(_bodyLength);

                        buffer = ms.ToArray();
                    }

                hasher.TransformBlock(buffer, 0, buffer.Length, null, 0);

                buffer = new byte[8192];

                int nRead;

                while (0 != (nRead = stream.Read(buffer, 0, buffer.Length)))
                {
                    hasher.TransformBlock(buffer, 0, nRead, null, 0);
                }

                hasher.TransformFinalBlock(buffer, 0, 0);

                if (stream.CanSeek)
                {
                    stream.Position = pos;
                }

                return(hasher.Hash);
            }
        }
Esempio n. 2
0
        byte[] CalculateHash(bool create)
        {
            if (create)
            {
                _hashLength = Length;
            }

            long oldPos = Position;

            Position = 0;
            using (HashAlgorithm hasher = QQnCryptoHelpers.CreateHashAlgorithm(_hashType))
            {
                byte[] buffer;
                using (MemoryStream ms = new MemoryStream(16))                 // Use memorystream and writer to resolve endian-issues
                    using (QQnBinaryWriter bw = new QQnBinaryWriter(ms))
                    {
                        bw.Write(_hashLength);
                        buffer = ms.ToArray();
                    }

                hasher.TransformBlock(buffer, 0, buffer.Length, null, 0);

                buffer = new byte[8192];

                int nRead;

                while (0 != (nRead = Read(buffer, 0, buffer.Length)))
                {
                    hasher.TransformBlock(buffer, 0, nRead, null, 0);
                }

                hasher.TransformFinalBlock(buffer, 0, 0);

                if (CanSeek)
                {
                    Position = oldPos;
                }

                return(hasher.Hash);
            }
        }