在Windows10.Visual Studio2019中编程。RFC2898编码过程,来自.NET Framowork 4.7.2项目。到Xamarin.Form (.NET标准2.0)。原始处理过程如下
var salt = "abcdefg";
var passWord = "password";
var iterations = 5;
var saltbyte = System.Text.Encoding.UTF8. GetBytes(salt);
var Rfc2898 = new System.Security.Cryptography. Rfc2898DeriveBytes(passWord, saltbyte, iterations, System.Security.Cryptography. HashAlgorithmName.SHA256);移植它将在Xamarin中导致错误,因为您不能指定散列算法。您不能指定哈希算法。
var Rfc2898 = new System.Security.Cryptography. Rfc2898DeriveBytes(passWord, saltbyte, iterations);如何在Xamarin中指定哈希算法?
用www.DeepL.com/Translator翻译(免费版)
发布于 2020-07-13 16:33:46
似乎.NET标准2.0没有为构造函数提供HashAlgorithmName参数。但是,它存在于.NET标准2.1中。您可以轻松地将库更改为使用.NET标准2.1。
否则,您将不得不专门使用该平台。Xamarin.iOS和Xamarin.Android确实有可用的构造函数。
因此,我将创建一个服务来散列您的密码:
public interface IPasswordHasher
{
byte[] GetHashedPassword(string password, string salt, int keySize);
}然后在Android和iOS上实现它,并将其注册为Xamarin.Forms DependencyService实例:
using System.Security.Cryptography;
using System.Text.Encoding;
using Xamarin.Forms;
[assembly: Dependency(typeof(MyAwesomeProject.iOS.Services.PasswordHasher))]
namespace MyAwesomeProject.iOS.Services
{
public class PasswordHasher : IPasswordHasher
{
public byte[] GetHashedPassword(string password, string salt, int keySize)
{
var saltbyte = UTF8.GetBytes(salt);
var rfc2898 = new Rfc2898DeriveBytes(password, saltbyte, 1000, HashAlgorithmName.SHA256);
return rfc2898.GetBytes(keySize);
}
}
}然后,当您需要将其用作:
var bytes = DependencyService.Get<IDeviceOrientationService>().GetHashedPassword("password", "abcdefg", 20);我强烈建议您在PBKDF2密钥派生中使用5次以上的迭代。默认值为1000,越高越好。当然,在移动设备上,你可能会遇到性能限制,但5太低了。
https://stackoverflow.com/questions/62870362
复制相似问题