HashAlgorithmProvider HashAlgorithmProvider HashAlgorithmProvider HashAlgorithmProvider HashAlgorithmProvider Class


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

public : sealed class HashAlgorithmProvider : IHashAlgorithmProvider
struct winrt::Windows::Security::Cryptography::Core::HashAlgorithmProvider : IHashAlgorithmProvider
public sealed class HashAlgorithmProvider : IHashAlgorithmProvider
Public NotInheritable Class HashAlgorithmProvider Implements IHashAlgorithmProvider
// This class does not provide a public constructor.
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)


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.

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


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

  • MD5
  • SHA1
  • SHA256
  • SHA384
  • SHA512


AlgorithmName AlgorithmName AlgorithmName AlgorithmName AlgorithmName

Gets the name of the open hash algorithm.

public : Platform::String AlgorithmName { get; }
winrt::hstring AlgorithmName();
public string AlgorithmName { get; }
Public ReadOnly Property AlgorithmName As string
var string = hashAlgorithmProvider.algorithmName;
string string string

Algorithm name.


The following hashing algorithm names are supported:

See Also

HashLength HashLength HashLength HashLength HashLength

Gets the length, in bytes, of the hash.

public : unsigned int HashLength { get; }
uint32_t HashLength();
public uint HashLength { get; }
Public ReadOnly Property HashLength As uint
var uint = hashAlgorithmProvider.hashLength;
uint uint uint

Number of bytes in the hash.


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.


CreateHash() CreateHash() CreateHash() CreateHash() CreateHash()

Creates a reusable CryptographicHash object.

public : CryptographicHash CreateHash()
CryptographicHash CreateHash() const;
public CryptographicHash CreateHash()
Public Function CreateHash() As CryptographicHash
var cryptographicHash = hashAlgorithmProvider.createHash();


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.

See Also

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

Hashes binary data.

public : IBuffer HashData(IBuffer data)
IBuffer HashData(IBuffer data) const;
public IBuffer HashData(IBuffer data)
Public Function HashData(data As IBuffer) As IBuffer
var iBuffer = hashAlgorithmProvider.hashData(data);
IBuffer IBuffer IBuffer

Data to be hashed.


Hashed data.


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

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

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

public : static HashAlgorithmProvider OpenAlgorithm(Platform::String algorithm)
HashAlgorithmProvider OpenAlgorithm(winrt::hstring algorithm) const;
public static HashAlgorithmProvider OpenAlgorithm(String algorithm)
Public Static Function OpenAlgorithm(algorithm As String) As HashAlgorithmProvider
var hashAlgorithmProvider = Windows.Security.Cryptography.Core.HashAlgorithmProvider.openAlgorithm(algorithm);
String String String

Algorithm name.



The following hashing algorithm names are supported:

See Also