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 对象并打开要使用的指定算法。

适用于

另请参阅