HashAlgorithm Class
.NET Framework Class Library
HashAlgorithm Class

Represents the base class from which all implementations of cryptographic hash algorithms must derive.

Namespace:  System.Security.Cryptography
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic
<ComVisibleAttribute(True)> _
Public MustInherit Class HashAlgorithm _
    Implements ICryptoTransform, IDisposable
C#
[ComVisibleAttribute(true)]
public abstract class HashAlgorithm : ICryptoTransform, 
    IDisposable
Visual C++
[ComVisibleAttribute(true)]
public ref class HashAlgorithm abstract : ICryptoTransform, 
    IDisposable
F#
[<AbstractClassAttribute>]
[<ComVisibleAttribute(true)>]
type HashAlgorithm =  
    class
        interface ICryptoTransform
        interface IDisposable
    end

Hash functions are fundamental to modern cryptography. These functions map binary strings of an arbitrary length to small binary strings of a fixed length, known as hash values. A cryptographic hash function has the property that it is computationally infeasible to find two distinct inputs that hash to the same value. Hash functions are commonly used with digital signatures and for data integrity.

The hash is used as a unique value of fixed size representing a large amount of data. Hashes of two sets of data should match if the corresponding data also matches. Small changes to the data result in large unpredictable changes in the hash.

Notes to Inheritors

When you inherit from the HashAlgorithm class, you must override the following members: HashCore and HashFinal.

The following code example computes the SHA1CryptoServiceProvider hash for an array. This example assumes that there is a predefined byte array dataArray[]. SHA1CryptoServiceProvider is a derived class of HashAlgorithm.

Visual Basic
Dim sha As New SHA1CryptoServiceProvider()
Dim result As Byte() = sha.ComputeHash(dataArray)
C#
HashAlgorithm sha = new SHA1CryptoServiceProvider();
byte[] result = sha.ComputeHash(dataArray);
Visual C++
HashAlgorithm^ sha = gcnew SHA1CryptoServiceProvider;
array<Byte>^ result = sha->ComputeHash( dataArray );
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role not supported), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
VB.NET File Integrity Checksum Hash      eriklitze   |   Edit   |   Show History
  
Imports System.Security.Cryptography
Imports System.IO
Public Class FileChecksum
Private Const BUF_SIZE As Integer = 65536
''' <summary>
''' Returns the file integrity checksum hash, otherwise an empty string.
''' </summary>
Public Shared Function IntegrityCheck(ByVal filePath As String) As String
Dim dataBuffer(BUF_SIZE - 1) As Byte
Dim dataBufferDummy(BUF_SIZE - 1) As Byte
Dim dataBytesRead As Integer = 0
Dim hashResult As String = String.Empty
Dim hashAlg As HashAlgorithm = Nothing
Dim fs As FileStream = Nothing
Try
hashAlg = New MD5CryptoServiceProvider ' or New SHA1CryptoServiceProvider
fs = New FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, BUF_SIZE)
Do
dataBytesRead = fs.Read(dataBuffer, 0, BUF_SIZE)
hashAlg.TransformBlock(dataBuffer, 0, dataBytesRead, dataBufferDummy, 0)
Loop Until dataBytesRead = 0
hashAlg.TransformFinalBlock(dataBuffer, 0, 0)
hashResult = BitConverter.ToString(hashAlg.Hash).Replace("-", "").ToLower
Catch ex As IOException
MsgBox(ex.Message, MsgBoxStyle.Critical, "IntegrityCheck")
Catch ex As UnauthorizedAccessException
MsgBox(ex.Message, MsgBoxStyle.Critical, "IntegrityCheck")
Finally
If Not fs Is Nothing Then
fs.Close()
fs.Dispose()
fs = Nothing
End If
If Not hashAlg Is Nothing Then
hashAlg.Clear() 'Dispose()
hashAlg = Nothing
End If
End Try
Return hashResult
End Function
End Class
Tags What's this?: Add a tag
Flag as ContentBug
Processing
Page view tracker