The .NET Framework represents all hashing
algorithms with the abstract
System.Security.Cryptography.HashAlgorithm class.
An abstract class represents each specific algorithm, extended by
individual implementation classes, as shown in Figure 1. This approach supports selecting between
several implementations of the same algorithm.
There are two types of implementation class included with the .NET
Framework. Those whose names end with Managed (for
example, SHA1Managed) were written in a managed
.NET language (for example, C# or Visual Basic .NET). Those classes
whose names end in CryptoServiceProvider (for
example, SHA1CryptoServiceProvider) rely on the
Windows Crypto API. Where there is a choice between a managed
implementation and a Crypto API implementation, our (unscientific)
testing shows that the Crypto API versions are slightly faster and
consume less system memory.
The Crypto API is a component of the Windows
operating system that contains cryptographic functionality. Many of
the .NET cryptographic classes rely on features of the Crypto API for
tasks such as key management or providing implementations of
algorithms. As a .NET programmer, you do not need to have an
understanding of the details of the Crypto API. We do not explore the
Crypto API in this book; for more information, consult the Windows
API documentation.
|
1. The HashAlgorithm Class
The design of the
HashAlgorithm class makes it very simple to generate
hash codes for any of the hashing algorithms that the .NET Framework
supports. In this section, we show you how to create an instance of a
given hashing algorithm and the techniques used to create hash codes
for different types of data. Table 1 lists the
members of the HashAlgorithm class.
Table 1. Members of the HashAlgorithm Class
Member
|
Description
|
---|
Properties
| |
Hash
|
This property returns the value of the computed hash code. It is not
commonly used. See the following sections for instructions on using
the ComputeHash method to create hash codes.
|
HashSize
|
This property returns the size of the hash code (in bits) that the
hashing algorithm produces.
|
CanReuseTransform
|
These properties are from the ICryptoTranform
interface, which is not directly related to hashing, but is
implemented by the HashAlgorithm class.
|
CanTransformMultipleBlocks
| |
InputBlockSize
| |
OutputBlockSize
| |
Methods
| |
Create
|
This static method creates a new instance of the
HashAlgorithm class by name. See Section 13.2.2 for further details.
|
ComputeHash
|
The ComputeHash method is used to create hash
codes from byte arrays and data streams. See Section 13.2.3 and Section 13.2.4 in this chapter for
full details.
|
Initialize
|
This is used by algorithm-implementation classes to initialize their
state before use.
|
Clear
|
This releases the resources held by the
HashAlgorithm instance.
|
TransformBlock
|
These methods are from the ICryptoTransform
interface, which is not directly related to hashing, but is
implemented by the HashAlgorithm class. |
TransformFinalBlock
| |