HashAlgorithmProvider HashAlgorithmProvider HashAlgorithmProvider HashAlgorithmProvider 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// You can use this class in JavaScript.
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

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

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

Properties

AlgorithmName 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// You can use this property in JavaScript.
Value
PlatForm::String string string string

Algorithm name.

Remarks

The following hashing algorithm names are supported:

See Also

HashLength HashLength HashLength HashLength

Gets the length, in bytes, of the hash.

public : unsigned int HashLength { get; }public uint HashLength { get; }Public ReadOnly Property HashLength As uint// You can use this property in JavaScript.
Value
unsigned int uint uint uint

Number of bytes in the hash.

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() CreateHash()

Creates a reusable CryptographicHash object.

public : CryptographicHash CreateHash()public CryptographicHash CreateHash()Public Function CreateHash() As CryptographicHash// You can use this method in JavaScript.
Returns

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.

See Also

HashData(IBuffer) 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// You can use this method in JavaScript.
Parameters
data
IBuffer IBuffer IBuffer IBuffer

Data to be hashed.

Returns

Remarks

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

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)public static HashAlgorithmProvider OpenAlgorithm(String algorithm)Public Static Function OpenAlgorithm(algorithm As String) As HashAlgorithmProvider// You can use this method in JavaScript.
Parameters
algorithm
PlatForm::String String String String

Algorithm name.

Returns

Represents a provider that implements hash algorithms.

Remarks

The following hashing algorithm names are supported:

See Also