Xamarin PCLCrypto SHA256提供不同的哈希 [英] Xamarin PCLCrypto SHA256 give different hash

查看:100
本文介绍了Xamarin PCLCrypto SHA256提供不同的哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Xamarin Forms XAML可移植项目.我需要从字符串获取SHA256.我使用PCLCrypto来查找我的哈希,因为PCL中不存在System.Security.Cryptography.

I have a project in Xamarin Forms XAML portable. i need to get a SHA256 from a string. I use the PCLCrypto for finding my hash, because System.Security.Cryptography doesn't exist in PCL.

我从API获得sha256以比较这些值.这是我用来获取SHA256哈希的方法:

I get a sha256 from an API to compare the values. This is the method I use to get the SHA256 hash:

private string getSha256(string data) {
            byte[] byteData = Encoding.UTF8.GetBytes(data);
            var hasher = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
            byte[] hash = hasher.HashData(byteData);
            string hashBase64 = Convert.ToBase64String(hash);
            return hashBase64;
        }

我使用的字符串是'secret'.我用它来检查它:

The string I use is 'secret'. I use this to check it:

http://www.xorbin.com/tools/sha256-hash-calculator 它给出:

2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b

但是我得到两个不同的哈希码:

But I'm getting two different hash codes:

API哈希: 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b

API hash: 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b

PCLCrypto哈希: K7gNU3sdo + OL0wNhqoVWhr3g6s1xYv72ol/pe/Unols =

PCLCrypto hash I generated: K7gNU3sdo+OL0wNhqoVWhr3g6s1xYv72ol/pe/Unols=

它具有相同的字符串"secret",但导致不同的哈希值.我还注意到PCLCrypto哈希始终以'='结尾.

Its the same string 'secret' but is resulting in different hashes. I also noticed PCLCrypto hash always ends with '='.

它们为什么不同?

推荐答案

如果使用类似 http://hash.online-convert.com/sha256-generator ,您会看到两个哈希是相同的(在文本"框中输入秘密",然后单击转换文件").

If you use a converter like http://hash.online-convert.com/sha256-generator you'll see that the two hashes are the same (enter 'secret' in the Text box and click on Convert File).

"2bb80 ..."字符串是哈希的十六进制编码,而"K7gNU ..."字符串是哈希的base64编码(这是您在调用PCLCrypto时所要求的).问题在于散列本身是二进制数据,因此要显示它,您需要将其转换为字符串.您会看到两种常用的方法,都使用相同的基础哈希.

The '2bb80...' string is the hex encoding of the hash, and the 'K7gNU...' string is the base64 encoding of the hash (which is what you asked for in your call to PCLCrypto). The issue is that the hash itself is binary data, so to display it you need some conversion to string. You're seeing two common ways to do that, both using the same underlying hash.

这篇关于Xamarin PCLCrypto SHA256提供不同的哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆