How to create SHA-256 hashes in WinRT?
Asked Answered
L

1

8

I went to create a simple one-way SHA-256 hash in WinRT today and realized it didn't work. I did a validation and apparently got this:

◦API System.Security.Cryptography.SHA256Managed in MSCORLIB, PUBLICKEYTOKEN=B77A5C561934E089 is not supported for this application type. CryptoWinRT.exe calls this API. ◦API System.Security.Cryptography.HashAlgorithm in MSCORLIB, PUBLICKEYTOKEN=B77A5C561934E089 is not supported for this application type. CryptoWinRT.exe calls this API. ◦API System.Security.Cryptography.SHA256Managed.#ctor in MSCORLIB, PUBLICKEYTOKEN=B77A5C561934E089 is not supported for this application type. CryptoWinRT.exe calls this API. ◦API System.Security.Cryptography.HashAlgorithm.ComputeHash(System.Byte[]) in MSCORLIB, PUBLICKEYTOKEN=B77A5C561934E089 is not supported for this application type. CryptoWinRT.exe calls this API.

What is the replacement for this? And why would such a trivial thing not be allowed in WinRT?

Lorrimor answered 19/9, 2012 at 21:17 Comment(1)
Duplicate of How do I perform a SHA512 hash in C++ WinRT? (Different hash algorithm, but the answer is the same.)Shikoku
F
20

Does this work for you?

    private void HandleHashClick(object sender, RoutedEventArgs e)
    {
        // get the text...
        var inputText = this.textInput.Text;

        // put the string in a buffer, UTF-8 encoded...
        IBuffer input = CryptographicBuffer.ConvertStringToBinary(inputText, 
            BinaryStringEncoding.Utf8);

        // hash it...
        var hasher = HashAlgorithmProvider.OpenAlgorithm("SHA256");
        IBuffer hashed = hasher.HashData(input);

        // format it...
        this.textBase64.Text = CryptographicBuffer.EncodeToBase64String(hashed);
        this.textHex.Text = CryptographicBuffer.EncodeToHexString(hashed);
    }
Furnace answered 19/10, 2012 at 19:36 Comment(1)
I actually ended up doing almost exactly this. I just never came back to here and posted an answerLorrimor

© 2022 - 2024 — McMap. All rights reserved.