HashAlgorithmProvider class

Applies to Windows and Windows Phone

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

Syntax


Public NotInheritable Class HashAlgorithmProvider

Attributes

[DualApiPartition()]
[MarshalingBehavior(Agile)]
[Threading(Both)]
[Version(0x06020000)]

Members

The HashAlgorithmProvider class has these types of members:

Methods

The HashAlgorithmProvider class has these methods. With C#, Visual Basic, and C++, it also inherits methods from the Object class.

MethodDescription
CreateHash Creates a reusable CryptographicHash object.
HashData Hashes binary data.
OpenAlgorithm Creates a HashAlgorithmProvider object and opens the specified algorithm for use.

 

Properties

The HashAlgorithmProvider class has these properties.

PropertyAccess typeDescription

AlgorithmName

Read-onlyGets the name of the open hash algorithm.

HashLength

Read-onlyGets the length, in bytes, of the hash.

 

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


Requirements

Minimum supported client

Windows 8 [Windows Store apps, desktop apps]

Minimum supported server

Windows Server 2012 [Windows Store apps, desktop apps]

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]

Namespace

Windows.Security.Cryptography.Core
Windows::Security::Cryptography::Core [C++]

Metadata

Windows.winmd

See also

Hashes and Digital Signatures

 

 

Show:
© 2014 Microsoft