Hash​Algorithm​Provider Hash​Algorithm​Provider Hash​Algorithm​Provider Class

Definition

Represents a cryptographic hash provider. For more information about hashes, see MACs, hashes, and signatures.

public sealed class HashAlgorithmProvider : IHashAlgorithmProviderpublic sealed class HashAlgorithmProvider : IHashAlgorithmProviderPublic NotInheritable Class HashAlgorithmProvider Implements IHashAlgorithmProvider
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Remarks

You create a HashAlgorithmProvider object by calling the static OpenAlgorithm method and specifying one of the following algorithm names:

  • MD5
  • SHA1
  • SHA256
  • SHA384
  • SHA512

Examples

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;
        }
    }
}

Properties

AlgorithmName AlgorithmName AlgorithmName

Gets the name of the open hash algorithm.

public PlatForm::String AlgorithmName { get; }public string AlgorithmName { get; }Public ReadOnly Property AlgorithmName As string
Value
string string string

Algorithm name.

Attributes

Remarks

The following hashing algorithm names are supported:

HashLength HashLength HashLength

Gets the length, in bytes, of the hash.

public unsigned short HashLength { get; }public uint HashLength { get; }Public ReadOnly Property HashLength As uint
Value
uint uint uint

Number of bytes in the hash.

Attributes

Remarks

You should verify that the length of the hashed data equals the hash length supported by the specified algorithm. For more information, see the following code example.

Methods

CreateHash() CreateHash() CreateHash()

Creates a reusable CryptographicHash object.

public CryptographicHash CreateHash()public CryptographicHash CreateHash()Public Function CreateHash() As CryptographicHash
Returns
Attributes

Remarks

You must call the OpenAlgorithm method before calling this property. Call the Append method on the CryptographicHash object to copy hashed data into a buffer. Call the GetValueAndReset method to retrieve the hashed data and reset the CryptographicHash object so that it can be used again to hash more data.

HashData(IBuffer) HashData(IBuffer) HashData(IBuffer)

Hashes binary data.

public IBuffer HashData(IBuffer data)public IBuffer HashData(IBuffer data)Public Function HashData(data As IBuffer) As IBuffer
Parameters
data
IBuffer IBuffer IBuffer

Data to be hashed.

Returns

Hashed data.

Attributes

Remarks

See the CryptographicHash class for information about creating a reusable hashing object.

OpenAlgorithm(String) OpenAlgorithm(String) OpenAlgorithm(String)

Creates a HashAlgorithmProvider object and opens the specified algorithm for use.

public static HashAlgorithmProvider OpenAlgorithm(String algorithm)public static HashAlgorithmProvider OpenAlgorithm(String algorithm)Public Static Function OpenAlgorithm(algorithm As String) As HashAlgorithmProvider
Parameters
algorithm
System.String System.String System.String

Algorithm name.

Returns

Represents a provider that implements hash algorithms.

Attributes

Remarks

The following hashing algorithm names are supported:

See Also