HashAlgorithmProvider 類別

定義

表示密碼編譯雜湊提供者。 如需雜湊的詳細資訊,請參閱 MAC、雜湊和簽章

public ref class HashAlgorithmProvider sealed
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class HashAlgorithmProvider final
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class HashAlgorithmProvider
Public NotInheritable Class HashAlgorithmProvider
繼承
Object Platform::Object IInspectable HashAlgorithmProvider
屬性

Windows 需求

裝置系列
Windows 10 (已於 10.0.10240.0 引進)
API contract
Windows.Foundation.UniversalApiContract (已於 v1.0 引進)

範例

using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using Windows.Storage.Streams;

namespace SampleHashAlgorithmProvider
{

    sealed partial class HashAlgProviderApp : Application
    {
        public HashAlgProviderApp()
        {
            // Initialize the application.
            this.InitializeComponent();

            // Hash a message.
            String strAlgName = HashAlgorithmNames.Sha512;
            String strMsg = "This is a message to be hashed.";
            String strEncodedHash = this.SampleHashMsg(strAlgName, strMsg);
        }

        public String SampleHashMsg(String strAlgName, String strMsg)
        {
            // Convert the message string to binary data.
            IBuffer buffUtf8Msg = CryptographicBuffer.ConvertStringToBinary(strMsg, BinaryStringEncoding.Utf8);

            // Create a HashAlgorithmProvider object.
            HashAlgorithmProvider objAlgProv = HashAlgorithmProvider.OpenAlgorithm(strAlgName);

            // Demonstrate how to retrieve the name of the hashing algorithm.
            String strAlgNameUsed = objAlgProv.AlgorithmName;

            // Hash the message.
            IBuffer buffHash = objAlgProv.HashData(buffUtf8Msg);

            // Verify that the hash length equals the length specified for the algorithm.
            if (buffHash.Length != objAlgProv.HashLength)
            {
                throw new Exception("There was an error creating the hash");
            }

            // Convert the hash to a string (for display).
            String strHashBase64 = CryptographicBuffer.EncodeToBase64String(buffHash);

            // Return the encoded string
            return strHashBase64;
        }
    }
}

備註

您可以呼叫靜態 OpenAlgorithm 方法並指定下列其中一個演算法名稱,以建立 HashAlgorithmProvider 物件:

  • MD5
  • SHA1
  • SHA256
  • SHA384
  • SHA512

屬性

AlgorithmName

取得開啟雜湊演算法的名稱。

HashLength

取得雜湊的長度,以位元組為單位。

方法

CreateHash()

建立可重複使用 的 CryptographicHash 物件。

HashData(IBuffer)

雜湊二進位資料。

OpenAlgorithm(String)

建立 HashAlgorithmProvider 物件,並開啟指定的演算法以供使用。

適用於

另請參閱