Exemplo n.º 1
0
            /// <summary>
            /// Hashes data from a stream.
            /// </summary>
            /// <param name="es">The input stream.</param>
            /// <param name="length">The number of bytes to hash.</param>
            /// <returns>The hashed digest.</returns>
            /// <remarks>
            /// The method will hash length bytes of the stream from the current position
            /// and the stream position will be restored before the method
            /// returns.
            /// </remarks>
            public static byte[] Compute(EnhancedStream es, long length)
            {
                MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                long streamPos;

                byte[] buf;
                int    cb;

                streamPos = es.Position;
                buf       = new byte[8192];

                while (length > 0)
                {
                    cb = (int)(length > buf.Length ? buf.Length : length);
                    if (es.Read(buf, 0, cb) < cb)
                    {
                        throw new InvalidOperationException("Read past end of stream.");
                    }

                    md5.TransformBlock(buf, 0, cb, buf, 0);
                    length -= cb;
                }

                md5.TransformFinalBlock(buf, 0, 0);
                es.Seek(streamPos, SeekOrigin.Begin);

                return(md5.Hash);
            }
Exemplo n.º 2
0
            /// <summary>
            /// Uses the HMAC/MD5 algorithm to hash data from a stream.
            /// </summary>
            /// <param name="key">The secret key.</param>
            /// <param name="es">The input stream.</param>
            /// <param name="length">The number of bytes to hash.</param>
            /// <returns>The hashed digest.</returns>
            /// <remarks>
            /// The method will hash length bytes of the stream from the current position.
            /// and the stream position will be restored before the method
            /// returns.
            /// </remarks>
            public static byte[] Compute(byte[] key, EnhancedStream es, int length)
            {
                byte[] hash;
                long   pos;

                pos  = es.Position;
                hash = Compute(key, es.ReadBytes(length));
                es.Seek(pos, SeekOrigin.Begin);

                return(hash);
            }