HashAlgorithm mit IDisposable aber ohne Dispose



  • Wie ist es möglich, dass die Klasse System.Security.Cryptography.HashAlgorithm zwar das Interface IDisposable implementiert, man aber keine Funktion void Dispose() dort findet? Es gibt zwar eine protected virtual void Dispose(bool disposing) , aber die entspricht ja nicht der Interfacedeklaration.

    Dass die Klasse abstrakt ist, damit kann es wohl nicht zusammenhängen. Denn die Klasse SHA256Managed leitet von SHA256 ab und SHA256 leitet von HashAlgorithm ab. Und keine davon implementiert void Dispose() .



  • Gambel schrieb:

    Wie ist es möglich, dass die Klasse System.Security.Cryptography.HashAlgorithm zwar das Interface IDisposable implementiert, man aber keine Funktion void Dispose() dort findet?

    Hier ist sie doch: http://msdn.microsoft.com/de-de/library/dd289076.aspx



  • Wo?

    #region Assembly mscorlib.dll, v2.0.50727
    // C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll
    #endregion
    
    using System;
    using System.IO;
    using System.Runtime.InteropServices;
    
    namespace System.Security.Cryptography
    {
        // Summary:
        //     Represents the base class from which all implementations of cryptographic
        //     hash algorithms must derive.
        [ComVisible(true)]
        public abstract class HashAlgorithm : ICryptoTransform, IDisposable
        {
            // Summary:
            //     Represents the size, in bits, of the computed hash code.
            protected int HashSizeValue;
            //
            // Summary:
            //     Represents the value of the computed hash code.
            protected internal byte[] HashValue;
            //
            // Summary:
            //     Represents the state of the hash computation.
            protected int State;
    
            // Summary:
            //     Initializes a new instance of the System.Security.Cryptography.HashAlgorithm
            //     class.
            protected HashAlgorithm();
    
            // Summary:
            //     Gets a value indicating whether the current transform can be reused.
            //
            // Returns:
            //     Always true.
            public virtual bool CanReuseTransform { get; }
            //
            // Summary:
            //     When overridden in a derived class, gets a value indicating whether multiple
            //     blocks can be transformed.
            //
            // Returns:
            //     true if multiple blocks can be transformed; otherwise, false.
            public virtual bool CanTransformMultipleBlocks { get; }
            //
            // Summary:
            //     Gets the value of the computed hash code.
            //
            // Returns:
            //     The current value of the computed hash code.
            //
            // Exceptions:
            //   System.Security.Cryptography.CryptographicUnexpectedOperationException:
            //     System.Security.Cryptography.HashAlgorithm.HashValue is null.
            //
            //   System.ObjectDisposedException:
            //     The object has already been disposed.
            public virtual byte[] Hash { get; }
            //
            // Summary:
            //     Gets the size, in bits, of the computed hash code.
            //
            // Returns:
            //     The size, in bits, of the computed hash code.
            public virtual int HashSize { get; }
            //
            // Summary:
            //     When overridden in a derived class, gets the input block size.
            //
            // Returns:
            //     The input block size.
            public virtual int InputBlockSize { get; }
            //
            // Summary:
            //     When overridden in a derived class, gets the output block size.
            //
            // Returns:
            //     The output block size.
            public virtual int OutputBlockSize { get; }
    
            // Summary:
            //     Releases all resources used by the System.Security.Cryptography.HashAlgorithm
            //     class.
            public void Clear();
            //
            // Summary:
            //     Computes the hash value for the specified byte array.
            //
            // Parameters:
            //   buffer:
            //     The input to compute the hash code for.
            //
            // Returns:
            //     The computed hash code.
            //
            // Exceptions:
            //   System.ArgumentNullException:
            //     buffer is null.
            //
            //   System.ObjectDisposedException:
            //     The object has already been disposed.
            public byte[] ComputeHash(byte[] buffer);
            //
            // Summary:
            //     Computes the hash value for the specified System.IO.Stream object.
            //
            // Parameters:
            //   inputStream:
            //     The input to compute the hash code for.
            //
            // Returns:
            //     The computed hash code.
            //
            // Exceptions:
            //   System.ObjectDisposedException:
            //     The object has already been disposed.
            public byte[] ComputeHash(Stream inputStream);
            //
            // Summary:
            //     Computes the hash value for the specified region of the specified byte array.
            //
            // Parameters:
            //   buffer:
            //     The input to compute the hash code for.
            //
            //   offset:
            //     The offset into the byte array from which to begin using data.
            //
            //   count:
            //     The number of bytes in the array to use as data.
            //
            // Returns:
            //     The computed hash code.
            //
            // Exceptions:
            //   System.ArgumentException:
            //     count is an invalid value.  -or- buffer length is invalid.
            //
            //   System.ArgumentNullException:
            //     buffer is null.
            //
            //   System.ArgumentOutOfRangeException:
            //     offset is out of range. This parameter requires a non-negative number.
            //
            //   System.ObjectDisposedException:
            //     The object has already been disposed.
            public byte[] ComputeHash(byte[] buffer, int offset, int count);
            //
            // Summary:
            //     Creates an instance of the default implementation of the System.Security.Cryptography.HashAlgorithm
            //     class.
            //
            // Returns:
            //     A cryptographic object to perform the hash algorithm.
            public static HashAlgorithm Create();
            //
            // Summary:
            //     Creates an instance of the specified implementation of the System.Security.Cryptography.HashAlgorithm
            //     class.
            //
            // Parameters:
            //   hashName:
            //     The implementation of System.Security.Cryptography.HashAlgorithm to create.
            //
            // Returns:
            //     A cryptographic object to perform the hash algorithm.
            public static HashAlgorithm Create(string hashName);
            //
            // Summary:
            //     Releases the unmanaged resources used by the System.Security.Cryptography.HashAlgorithm
            //     and optionally releases the managed resources.
            //
            // Parameters:
            //   disposing:
            //     true to release both managed and unmanaged resources; false to release only
            //     unmanaged resources.
            protected virtual void Dispose(bool disposing);
            //
            // Summary:
            //     When overridden in a derived class, routes data written to the object into
            //     the hash algorithm for computing the hash.
            //
            // Parameters:
            //   array:
            //     The input to compute the hash code for.
            //
            //   ibStart:
            //     The offset into the byte array from which to begin using data.
            //
            //   cbSize:
            //     The number of bytes in the byte array to use as data.
            protected abstract void HashCore(byte[] array, int ibStart, int cbSize);
            //
            // Summary:
            //     When overridden in a derived class, finalizes the hash computation after
            //     the last data is processed by the cryptographic stream object.
            //
            // Returns:
            //     The computed hash code.
            protected abstract byte[] HashFinal();
            //
            // Summary:
            //     Initializes an implementation of the System.Security.Cryptography.HashAlgorithm
            //     class.
            public abstract void Initialize();
            //
            // Summary:
            //     Computes the hash value for the specified region of the input byte array
            //     and copies the resulting hash value to the specified region of the output
            //     byte array.
            //
            // Parameters:
            //   inputBuffer:
            //     The input to compute the hash code for.
            //
            //   inputOffset:
            //     The offset into the input byte array from which to begin using data.
            //
            //   inputCount:
            //     The number of bytes in the input byte array to use as data.
            //
            //   outputBuffer:
            //     A copy of the part of the input array used to compute the hash code.
            //
            //   outputOffset:
            //     The offset into the output byte array from which to begin writing data.
            //
            // Returns:
            //     The number of bytes written.
            //
            // Exceptions:
            //   System.ArgumentException:
            //     inputCount uses an invalid value.  -or- inputBuffer has an invalid length.
            //
            //   System.ArgumentNullException:
            //     inputBuffer is null.
            //
            //   System.ArgumentOutOfRangeException:
            //     inputOffset is out of range. This parameter requires a non-negative number.
            //
            //   System.ObjectDisposedException:
            //     The object has already been disposed.
            public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset);
            //
            // Summary:
            //     Computes the hash value for the specified region of the specified byte array.
            //
            // Parameters:
            //   inputBuffer:
            //     The input to compute the hash code for.
            //
            //   inputOffset:
            //     The offset into the byte array from which to begin using data.
            //
            //   inputCount:
            //     The number of bytes in the byte array to use as data.
            //
            // Returns:
            //     An array that is a copy of the part of the input that is hashed.
            //
            // Exceptions:
            //   System.ArgumentException:
            //     inputCount uses an invalid value.  -or- inputBuffer has an invalid offset
            //     length.
            //
            //   System.ArgumentNullException:
            //     inputBuffer is null.
            //
            //   System.ArgumentOutOfRangeException:
            //     inputOffset is out of range. This parameter cannot be a non-negative number.
            //
            //   System.ObjectDisposedException:
            //     The object has already been disposed.
            public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount);
        }
    }
    


  • ILSpy:

    // System.Security.Cryptography.HashAlgorithm
    /// <summary>Gibt alle von der aktuellen Instanz der <see cref="T:System.Security.Cryptography.HashAlgorithm" />-Klasse verwendeten Ressourcen frei.</summary>
    [SecuritySafeCritical]
    public void Dispose()
    {
    	this.Dispose(true);
    	GC.SuppressFinalize(this);
    }
    


  • Wieso findet sich die Funktion nicht in der Deklaration?



  • 😕

    Es gibt keine getrennte Deklaration und Definition in C#.

    Das da oben ist aus dem Metadaten rekonstruiert. Und wenn Visual Studio sich dazu entscheidet, Dispose nicht darzustellen, ist das nunmal so. IDisposable ist ein Sonderfall und nicht irgendeine Schnittstelle. Der Compiler kennt das interface wegen der using-Blöcke. Vielleicht hat man sich wegen dieser engen Verzahnung mit der Sprache einfach dafür entschieden, Dispose bei Abruf der Metadaten nicht aufzulisten.

    Nur geraten. Eigentlich auch nicht soooo wichtig, oder? 😉



  • µ schrieb:

    Eigentlich auch nicht soooo wichtig, oder? 😉

    Wichtig nicht, aber interessant schon. Wieso sollte man Dispose bei den Metadaten weglassen, aber in der MSDN aufführen?



  • Vielleicht weil es explizit implementiert ist?

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

    🙄


    Edit: Und nur deswegen wird es nicht einfach so angezeigt. Das hat auch nichts mit dem using zu tun. Außerdem wird Dispose bei anderen Klassen auch so angezeigt.


Anmelden zum Antworten