如何在 Xamarin 中使用哈希 SHA

How to use Hash SHA in Xamarin

我想在 Xamarin 中用 C# 散列一个字符串。 通常我会使用:

using System.Security.Cryptography;

public string SHA512StringHash(String input)
{
    SHA512 shaM = new SHA512Managed();
    // Convert the input string to a byte array and compute the hash.
    byte[] data = shaM.ComputeHash(Encoding.UTF8.GetBytes(input));
    // Create a new Stringbuilder to collect the bytes
    // and create a string.
    StringBuilder sBuilder = new StringBuilder();
    // Loop through each byte of the hashed data 
    // and format each one as a hexadecimal string.
    for (int i = 0; i < data.Length; i++)
    {
        sBuilder.Append(data[i].ToString("x2"));
    }
    // Return the hexadecimal string.
    input = sBuilder.ToString();
    return (input);
}

但在 Xamarin 中,我似乎无法包含此库。 有没有办法包含此库或轻松散列密码的替代方法?

我不知道为什么这行不通。我创建了一个示例项目来测试它,它运行良好。我唯一要做的就是添加对以下内容的引用:

using System.Security.Cryptography;

将此行添加到文件的顶部,它应该可以解决问题。或者右击没有被识别的对象,让XamStudio或者VS帮你解决,方法如下:

我找到了解决方案, 我试图在表单解决方案中创建这个 class,但在这个解决方案中 using System.Security.Cryptography; 不可用。 在 iOS 和 Android 解决方案中可用。 所以我在表单解决方案中实现了一个接口,并在 iOS 和 Android 解决方案中使用了方法。

System.Security.Cryptography 在 PCL 个项目中不可用。您可以使用 pclcontrib nuget 在 PCL 项目中添加 System.Security.Cryptography。 http://pclcontrib.codeplex.com