HashAlgorithmProvider
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 : 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.
- 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 AlgorithmName |
Gets the name of the open hash algorithm. |
HashLength HashLength HashLength HashLength HashLength |
Gets the length, in bytes, of the hash. |
Methods
CreateHash() CreateHash() CreateHash() CreateHash() CreateHash() |
Creates a reusable CryptographicHash object. |
HashData(IBuffer) HashData(IBuffer) HashData(IBuffer) HashData(IBuffer) HashData(IBuffer) |
Hashes binary data. |
OpenAlgorithm(String) OpenAlgorithm(String) OpenAlgorithm(String) OpenAlgorithm(String) OpenAlgorithm(String) |
Creates a HashAlgorithmProvider object and opens the specified algorithm for use. |