HashAlgorithm

[System.Runtime.InteropServices.ComVisible(true)]
public abstract class HashAlgorithm : IDisposable, ICryptoTransform
{
    protected HashAlgorithm() { }


    public void Clear()
    {
        (this as IDisposable).Dispose();
    }

#if FEATURE_CORECLR
        void IDisposable.Dispose()
        {
            Dispose();
        }
#endif // FEATURE_CORECLR

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (HashValue != null)
                Array.Clear(HashValue, 0, HashValue.Length);
            HashValue = null;
            m_bDisposed = true;
        }
    }


    static public HashAlgorithm Create()
    {
        return Create("System.Security.Cryptography.HashAlgorithm");
    }

    static public HashAlgorithm Create(String hashName)
    {
        return (HashAlgorithm)CryptoConfig.CreateFromName(hashName);
    }

    public byte[] ComputeHash(Stream inputStream)
    {
        if (m_bDisposed)
            throw new ObjectDisposedException(null);

        // Default the buffer size to 4K.
        byte[] buffer = new byte[4096];
        int bytesRead;
        do
        {
            bytesRead = inputStream.Read(buffer, 0, 4096);
            if (bytesRead > 0)
            {
                HashCore(buffer, 0, bytesRead);
            }
        } while (bytesRead > 0);

        HashValue = HashFinal();
        byte[] Tmp = (byte[])HashValue.Clone();
        Initialize();
        return (Tmp);
    }

    public byte[] ComputeHash(byte[] buffer)
    {
        if (m_bDisposed)
            throw new ObjectDisposedException(null);

        // Do some validation
        if (buffer == null) throw new ArgumentNullException("buffer");

        HashCore(buffer, 0, buffer.Length);
        HashValue = HashFinal();
        byte[] Tmp = (byte[])HashValue.Clone();
        Initialize();
        return (Tmp);
    }

    public byte[] ComputeHash(byte[] buffer, int offset, int count)
    {
        // Do some validation
        if (buffer == null)
            throw new ArgumentNullException("buffer");
        if (offset < 0)
            throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
        if (count < 0 || (count > buffer.Length))
            throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue"));
        if ((buffer.Length - count) < offset)
            throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
        Contract.EndContractBlock();

        if (m_bDisposed)
            throw new ObjectDisposedException(null);

        HashCore(buffer, offset, count);
        HashValue = HashFinal();
        byte[] Tmp = (byte[])HashValue.Clone();
        Initialize();
        return (Tmp);
    }
}

 

posted @ 2016-11-03 22:07  茗::流  阅读(187)  评论(0)    收藏  举报
如有雷同,纯属参考。如有侵犯你的版权,请联系我。