如何在WinRT中创建SHA-256哈希?

Ear*_*rlz 8 .net windows-8 windows-runtime windows-store

我今天在WinRT中创建了一个简单的单向SHA-256哈希,并意识到它不起作用.我做了验证,显然得到了这个:

◦APISystem.Security.Cryptography.SHA256Managed in MSCORLIB,PUBLICKEYTOKEN = B77A5C561934E089不支持此应用程序类型.CryptoWinRT.exe调用此API.此应用程序类型不支持MSCORLIB中的API System.Security.Cryptography.HashAlgorithm,PUBLICKEYTOKEN = B77A5C561934E089.CryptoWinRT.exe调用此API.◦APISystem.Security.Cryptography.SHA256Managed.此应用程序类型不支持MSCORLIB中的#ctor,PUBLICKEYTOKEN = B77A5C561934E089.CryptoWinRT.exe调用此API.此应用程序类型不支持MSCORLIB中的API System.Security.Cryptography.HashAlgorithm.ComputeHash(System.Byte []),PUBLICKEYTOKEN = B77A5C561934E089.CryptoWinRT.exe调用此API.

这有什么替代品?为什么在WinRT中不允许这样一个微不足道的事情?

小智 18

这对你有用吗?

    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);
    }
Run Code Online (Sandbox Code Playgroud)