HashAlgorithm Class

Definition

代表基底類別,密碼編譯雜湊演算法的所有實作必須從它衍生。Represents the base class from which all implementations of cryptographic hash algorithms must derive.

public ref class HashAlgorithm abstract : IDisposable, System::Security::Cryptography::ICryptoTransform
public ref class HashAlgorithm abstract : IDisposable
public ref class HashAlgorithm abstract : System::Security::Cryptography::ICryptoTransform
public abstract class HashAlgorithm : IDisposable, System.Security.Cryptography.ICryptoTransform
public abstract class HashAlgorithm : IDisposable
public abstract class HashAlgorithm : System.Security.Cryptography.ICryptoTransform
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class HashAlgorithm : System.Security.Cryptography.ICryptoTransform
type HashAlgorithm = class
    interface IDisposable
    interface ICryptoTransform
type HashAlgorithm = class
    interface IDisposable
type HashAlgorithm = class
    interface ICryptoTransform
    interface IDisposable
Public MustInherit Class HashAlgorithm
Implements ICryptoTransform, IDisposable
Public MustInherit Class HashAlgorithm
Implements IDisposable
Public MustInherit Class HashAlgorithm
Implements ICryptoTransform
Inheritance
HashAlgorithm
Derived
Attributes
Implements

Examples

下列程式碼範例會計算陣列的 SHA1CryptoServiceProvider 雜湊。The following code example computes the SHA1CryptoServiceProvider hash for an array. 這個範例假設有一個預先定義的位元組陣列 dataArray[]This example assumes that there is a predefined byte array dataArray[]. SHA1CryptoServiceProviderHashAlgorithm的衍生類別。SHA1CryptoServiceProvider is a derived class of HashAlgorithm.

HashAlgorithm^ sha = gcnew SHA1CryptoServiceProvider;
array<Byte>^ result = sha->ComputeHash( dataArray );
HashAlgorithm sha = new SHA1CryptoServiceProvider();
byte[] result = sha.ComputeHash(dataArray);
Dim sha As New SHA1CryptoServiceProvider()
Dim result As Byte() = sha.ComputeHash(dataArray)

Remarks

雜湊函數是新式密碼編譯的基礎。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.

由於 SHA1 的衝突問題,Microsoft 建議您使用以 SHA256 或更好的加密方式為基礎的安全性模型。Due to collision problems with SHA1, Microsoft recommends a security model based on SHA256 or better.

Notes to Implementers

當您從 HashAlgorithm 類別繼承時,必須覆寫下列成員: HashCore(Byte[], Int32, Int32)HashFinal()When you inherit from the HashAlgorithm class, you must override the following members: HashCore(Byte[], Int32, Int32) and HashFinal().

Constructors

HashAlgorithm()

HashAlgorithm 類別的新執行個體初始化。Initializes a new instance of the HashAlgorithm class.

Fields

HashSizeValue

代表計算出來之雜湊碼的大小,以位元為單位。Represents the size, in bits, of the computed hash code.

HashValue

表示計算出來的雜湊碼的值。Represents the value of the computed hash code.

State

表示雜湊計算的狀態。Represents the state of the hash computation.

Properties

CanReuseTransform

取得值,表示目前的轉換是否可重複使用。Gets a value indicating whether the current transform can be reused.

CanTransformMultipleBlocks

在衍生類別中覆寫時,取得值以指出是否可以轉換多個區塊。When overridden in a derived class, gets a value indicating whether multiple blocks can be transformed.

Hash

取得計算出來之雜湊碼的值。Gets the value of the computed hash code.

HashSize

取得計算出來之雜湊碼的大小,以位元為單位。Gets the size, in bits, of the computed hash code.

InputBlockSize

在衍生類別中覆寫時,取得輸入區塊的大小。When overridden in a derived class, gets the input block size.

OutputBlockSize

在衍生類別中覆寫時,取得輸出區塊的大小。When overridden in a derived class, gets the output block size.

Methods

Clear()

釋放 HashAlgorithm 類別使用的所有資源。Releases all resources used by the HashAlgorithm class.

ComputeHash(Byte[])

計算指定位元組陣列的雜湊值。Computes the hash value for the specified byte array.

ComputeHash(Byte[], Int32, Int32)

計算所指定位元組陣列中指定區域的雜湊值。Computes the hash value for the specified region of the specified byte array.

ComputeHash(Stream)

計算指定 Stream 物件的雜湊值。Computes the hash value for the specified Stream object.

ComputeHashAsync(Stream, CancellationToken)
Create()

建立雜湊演算法的預設實作執行個體。Creates an instance of the default implementation of a hash algorithm.

Create(String)

建立雜湊演算法之指定實作的執行個體。Creates an instance of the specified implementation of a hash algorithm.

Dispose()

釋放目前的 HashAlgorithm 類別執行個體所使用的所有資源。Releases all resources used by the current instance of the HashAlgorithm class.

Dispose(Boolean)

釋放 HashAlgorithm 使用的 Unmanaged 資源,並且選擇性地釋放 Managed 資源。Releases the unmanaged resources used by the HashAlgorithm and optionally releases the managed resources.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
HashCore(Byte[], Int32, Int32)

在衍生類別中覆寫時,將寫入物件的資料轉遞到用來計算雜湊的雜湊演算法。When overridden in a derived class, routes data written to the object into the hash algorithm for computing the hash.

HashCore(ReadOnlySpan<Byte>)

將寫入物件的資料路由傳送至雜湊演算法,以用來計算雜湊。Routes data written to the object into the hash algorithm for computing the hash.

HashFinal()

於衍生類型中覆寫時,在密碼編譯雜湊演算法處理最後一筆資料後,完成雜湊計算。When overridden in a derived class, finalizes the hash computation after the last data is processed by the cryptographic hash algorithm.

Initialize()

將雜湊演算法重設為其初始狀態。Resets the hash algorithm to its initial state.

MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)
TransformBlock(Byte[], Int32, Int32, Byte[], Int32)

計算輸入位元組陣列中指定區域的雜湊值,並將指定的輸入位元組陣列區域複製到指定的輸出位元組陣列區域。Computes the hash value for the specified region of the input byte array and copies the specified region of the input byte array to the specified region of the output byte array.

TransformFinalBlock(Byte[], Int32, Int32)

計算所指定位元組陣列中指定區域的雜湊值。Computes the hash value for the specified region of the specified byte array.

TryComputeHash(ReadOnlySpan<Byte>, Span<Byte>, Int32)

嘗試計算指定位元組陣列的雜湊值。Attempts to compute the hash value for the specified byte array.

TryHashFinal(Span<Byte>, Int32)

在雜湊演算法處理最後一筆資料之後,嘗試完成雜湊計算。Attempts to finalize the hash computation after the last data is processed by the hash algorithm.

Explicit Interface Implementations

IDisposable.Dispose()

釋放 HashAlgorithm 使用的 Unmanaged 資源,並且選擇性地釋放 Managed 資源。Releases the unmanaged resources used by the HashAlgorithm and optionally releases the managed resources.

Applies to

See also