2012-09-19 34 views
7

我今天在WinRT中創建了一個簡單的單向SHA-256哈希,並意識到它不起作用。我做了驗證,顯然得到了這個:如何在WinRT中創建SHA-256散列?

◦APISystem.Security.Cryptography.SHA256Managed在MSCORLIB,不支持此應用程序 類型 公鑰= B77A5C561934E089。 CryptoWinRT.exe調用此API。 ◦API MSCORLIB中的System.Security.Cryptography.HashAlgorithm, PUBLICKEYTOKEN = B77A5C561934E089不適用於此應用程序 類型。 CryptoWinRT.exe調用此API。 ◦API System.Security.Cryptography.SHA256Managed。#ctor in MSCORLIB, PUBLICKEYTOKEN = B77A5C561934E089不適用於此應用程序 類型。 CryptoWinRT.exe調用此API。 ◦API 在MSCORLIB中,System.Security.Cryptography.HashAlgorithm.ComputeHash(System.Byte []) ,此 應用程序類型不支持PUBLICKEYTOKEN = B77A5C561934E089。 CryptoWinRT.exe調用此API。

這是什麼替代?爲什麼在WinRT中不允許這樣一件小事?

+0

複製的[如何執行在C++的WinRT一個SHA512散列?](http://stackoverflow.com/questions/12355417/how-do-i-perform-a-sha512-hash-in- c-winrt)(不同的哈希算法,但答案是一樣的。) –

回答

17

這是否適合您?

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); 
    } 
+0

我實際上最終做了這個。我從來沒有回到這裏,併發布了答案 – Earlz