Click here to Skip to main content
13,894,726 members
Click here to Skip to main content
Add your own
alternative version


8 bookmarked
Posted 22 Sep 2012
Licenced CPOL

Calculating hash values in Windows Metro Style applications using C#

, 4 Mar 2013
Rate this:
Please Sign up or sign in to vote.
Calculating hash values in Windows Metro style applications using C# and .NET 4.5.


This tip provides you a simple way to create a hash value for a string in Windows Metro style applications.


In cryptography, hash functions provide three separate functions:

  1. Collision resistance: How hard is it for someone to find two messages (any two messages) that hash the same?
  2. Pre-image resistance: Given a hash, how hard is it to find another message that hashes the same? Also known as a one way hash function.
  3. Second pre-image resistance: Given a message, find another message that hashes the same.

Windows Metro Style applications support the below hashing methods:

  • MD5
  • SHA1
  • SHA256
  • SHA384
  • SHA512

The normal .NET System.Security.Cryptography namespace does not exist in Metro thus we need to use the following namespaces:

  • Windows.Security.Cryptography;
  •  Windows.Security.Cryptography.Core;
  • Windows.Storage.Streams;

Using the Code 

As the first step, we need to create the hash algorithm provider object as an argument. For the Open Algorithm method, you can pass the hash type of your choice (MD5, SHA1, SHA256, SHA384, and SHA512).

HashAlgorithmProvider Algorithm = HashAlgorithmProvider.OpenAlgorithm("SHA256");

As the second step create a buffer using the CryptographicBuffer class and convert the value that you want to hash.

IBuffer vector = CryptographicBuffer.ConvertStringToBinary(DataString, BinaryStringEncoding.Utf8); 

Using the Algorithm object that you created to hash the data:

IBuffer digest = Algorithm.HashData(vector);

Check for validation:

if (digest.Length != Algorithm.HashLength){
    throw new System.InvalidOperationException("HashAlgorithmProvider failed to generate a hash of proper length!");            

Finally get the hash values as a hex string:

dataHash = CryptographicBuffer.EncodeToHexString(digest);
/// <summary>
/// This Method whill create a  Hash for a Given string
/// </summary>
/// <param name="DataString"></param>
/// <returns></returns>
internal string CalculateHashForString(string DataString,string hashType )
    string dataHash = string.Empty;

    if (string.IsNullOrWhiteSpace(DataString))
        return null;

    if (string.IsNullOrWhiteSpace(hashType))
        hashType = "MD5";
        ///Hash Algorithm Provider is Created 
        HashAlgorithmProvider Algorithm = HashAlgorithmProvider.OpenAlgorithm(hashType);
        ///Creating a Buffer Stream using the Cryptographic Buffer class and UTF8 encoding 
        IBuffer vector = CryptographicBuffer.ConvertStringToBinary(DataString, BinaryStringEncoding.Utf8);

        IBuffer digest = Algorithm.HashData(vector);////Hashing The Data 

        if (digest.Length != Algorithm.HashLength)
            throw new System.InvalidOperationException(
              "HashAlgorithmProvider failed to generate a hash of proper length!");            

            dataHash = CryptographicBuffer.EncodeToHexString(digest);//Encoding it to a Hex String 
        return dataHash;
    catch (Exception ex)

    return null;

private void Button_Click_1(object sender, RoutedEventArgs e)
            // Hash Types Supported

            string hashAlgorithm = "SHA256";
            lblAlg.Text = hashAlgorithm;
            lblResult.Text = CalculateHashForString(txtValue.Text, hashAlgorithm);
    }catch(Exception ex)

Points of Interest

Please note the sample code is written in .NET 4.5 and it’s a Metro style application.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Tyronne Thomas
Software Developer (Senior)
Sri Lanka Sri Lanka
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionNice but... Pin
wendas29-Jun-13 11:45
memberwendas29-Jun-13 11:45 
This is real nice, you kept the code simple and uncomplicated... But... Would have loved to see how to decode it, so we had the flipside of taking the encoded password and decoding the password.. I'm now trying to struggle with this.
QuestionNice Pin
Muigai Mwaura1-Nov-12 0:03
memberMuigai Mwaura1-Nov-12 0:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190306.1 | Last Updated 4 Mar 2013
Article Copyright 2012 by Tyronne Thomas
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid