Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
868 views
in Technique[技术] by (71.8m points)

.net - How to create SHA-256 hashes in WinRT?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...