CryptographicHash class

Expand
0 out of 1 rated this helpful - Rate this topic

CryptographicHash class

[This documentation is preliminary and is subject to change.]

Applies to: Metro style apps | desktop apps

Represents a reusable hashing object and contains the result of a hashing operation.

Syntax


var cryptographicHash = createHash();

Attributes

DualApiPartitionAttribute()
MarshalingBehaviorAttribute(Agile)
VersionAttribute(NTDDI_WIN8)

Members

The CryptographicHash class has these types of members:

Methods

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

MethodDescription
Append Copies hashed data to the CryptographicHash object.
GetValueAndReset Gets hashed data from the CryptographicHash object and resets the object.

 

Remarks

Call the CreateHash method on a HashAlgorithmProvider object to create a CryptographicHash object. You can open a hash algorithm provider, by name, for any of the following algorithm names:

  • MD5
  • SHA1
  • SHA256
  • SHA384
  • SHA512

Examples


public void SampleReusableHash()
{
    // Create a string that contains the name of the hashing algorithm to use.
    String strAlgName = "SHA512";

    // Create a HashAlgorithmProvider object.
    HashAlgorithmProvider objAlgProv = HashAlgorithmProvider.OpenAlgorithm(strAlgName);

    // Create a CryptographicHash object. This object can be reused to continually
    // hash new messages.
    CryptographicHash objHash = objAlgProv.CreateHash();

    // Hash message 1.
    String strMsg1 = "This is message 1.";
    IBuffer buffMsg1 = CryptographicBuffer.ConvertStringToBinary(strMsg1, BinaryStringEncoding.Utf16BE);
    objHash.Append(buffMsg1);
    IBuffer buffHash1 = objHash.GetValueAndReset();

    // Hash message 2.
    String strMsg2 = "This is message 2.";
    IBuffer buffMsg2 = CryptographicBuffer.ConvertStringToBinary(strMsg2, BinaryStringEncoding.Utf16BE);
    objHash.Append(buffMsg2);
    IBuffer buffHash2 = objHash.GetValueAndReset();

    // Hash message 3.
    String strMsg3 = "This is message 3.";
    IBuffer buffMsg3 = CryptographicBuffer.ConvertStringToBinary(strMsg3, BinaryStringEncoding.Utf16BE);
    objHash.Append(buffMsg3);
    IBuffer buffHash3 = objHash.GetValueAndReset();

    // Convert the hashes to string values (for display);
    String strHash1 = CryptographicBuffer.EncodeToBase64String(buffHash1);
    String strHash2 = CryptographicBuffer.EncodeToBase64String(buffHash2);
    String strHash3 = CryptographicBuffer.EncodeToBase64String(buffHash3);
}


Requirements

Minimum supported client

Windows 8 Release Preview

Minimum supported server

Windows Server 2012

Namespace

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

Metadata

Windows.winmd

See also

HashAlgorithmProvider
CreateHash

 

 

Build date: 5/22/2012

Did you find this helpful?
(1500 characters remaining)
Community Additions ADD