Computing hashes in WinRT

If you ever generated a MD5 or SHA hash in C# you problaby uset the classes from the System.Security.Cryptography namespace. This namespace is not available in WinRT, you have to use the Windows.Security.Cryptography.Core instead. This namespace contians a class called HashAlgorithmProvider that can be used to generate hashes using MD5 and SHA (SHA1, SHA256, SHA384, SHA512). A simple method to generate a hash for a string using a given algorithm may look like this

public static string GetHash(string algoritm, string s)
{
    HashAlgorithmProvider alg = HashAlgorithmProvider.OpenAlgorithm(algoritm);
    IBuffer buff = CryptographicBuffer.ConvertStringToBinary(s, BinaryStringEncoding.Utf8);
    var hashed = alg.HashData(buff);
    var res = CryptographicBuffer.EncodeToHexString(hashed);
    return res;
}

[Read More]