将 PCL 库哈希代码重写为新的 .NET 标准库时出现问题
Issues with rewriting PCL Library Hashing code to the new .NET Standard Library
基本上,我需要重写以下方法,这些方法适用于较旧的 PCL 库项目,但不适用于 .NET 标准库项目。
public static string GenerateSalt()
{
var buf = new byte[16];
(new RNGCryptoServiceProvider()).GetBytes(buf);
return Convert.ToBase64String(buf);
}
public static string GenerateHash(string password, string salt)
{
byte[] bytes = Encoding.Unicode.GetBytes(password);
byte[] src = Convert.FromBase64String(salt);
byte[] dst = new byte[src.Length + bytes.Length];
System.Buffer.BlockCopy(src, 0, dst, 0, src.Length);
System.Buffer.BlockCopy(bytes, 0, dst, src.Length, bytes.Length);
HashAlgorithm algorithm = HashAlgorithm.Create("SHA1");
byte[] inArray = algorithm.ComputeHash(dst);
return Convert.ToBase64String(inArray);
}
到目前为止,我已经尝试使用 RandomNumberGenerator.Create()
和 RandomNumberGenerator.GetBytes()
方法取得进展,但没有成功。
我收到错误解释 RandomNumberGenerator
是一个接口,因此无法创建实例(这是可以理解的),但我想必须有一种方法(我不是很有经验.Net & C#).
尝试做类似的事情:
public static string GenerateSalt()
{
var buf = new byte[16];
RandomNumberGenerator.Create().GetBytes(buf);
return Convert.ToBase64String(buf);
}
public static string GenerateHash(string password, string salt)
{
byte[] bytes = Encoding.Unicode.GetBytes(password); //TODO: consider removing it
byte[] src = Convert.FromBase64String(salt);
byte[] dst = new byte[src.Length + bytes.Length];
System.Buffer.BlockCopy(src, 0, dst, 0, src.Length);
System.Buffer.BlockCopy(bytes, 0, dst, src.Length, bytes.Length);
var provider = new Rfc2898DeriveBytes(password, src, 1000);
byte[] inArray = provider.GetBytes(20/*bytes like in SHA-1*/);
return Convert.ToBase64String(inArray);
}
或检查 1.4 版本可用的其他 API here
基本上,我需要重写以下方法,这些方法适用于较旧的 PCL 库项目,但不适用于 .NET 标准库项目。
public static string GenerateSalt()
{
var buf = new byte[16];
(new RNGCryptoServiceProvider()).GetBytes(buf);
return Convert.ToBase64String(buf);
}
public static string GenerateHash(string password, string salt)
{
byte[] bytes = Encoding.Unicode.GetBytes(password);
byte[] src = Convert.FromBase64String(salt);
byte[] dst = new byte[src.Length + bytes.Length];
System.Buffer.BlockCopy(src, 0, dst, 0, src.Length);
System.Buffer.BlockCopy(bytes, 0, dst, src.Length, bytes.Length);
HashAlgorithm algorithm = HashAlgorithm.Create("SHA1");
byte[] inArray = algorithm.ComputeHash(dst);
return Convert.ToBase64String(inArray);
}
到目前为止,我已经尝试使用 RandomNumberGenerator.Create()
和 RandomNumberGenerator.GetBytes()
方法取得进展,但没有成功。
我收到错误解释 RandomNumberGenerator
是一个接口,因此无法创建实例(这是可以理解的),但我想必须有一种方法(我不是很有经验.Net & C#).
尝试做类似的事情:
public static string GenerateSalt()
{
var buf = new byte[16];
RandomNumberGenerator.Create().GetBytes(buf);
return Convert.ToBase64String(buf);
}
public static string GenerateHash(string password, string salt)
{
byte[] bytes = Encoding.Unicode.GetBytes(password); //TODO: consider removing it
byte[] src = Convert.FromBase64String(salt);
byte[] dst = new byte[src.Length + bytes.Length];
System.Buffer.BlockCopy(src, 0, dst, 0, src.Length);
System.Buffer.BlockCopy(bytes, 0, dst, src.Length, bytes.Length);
var provider = new Rfc2898DeriveBytes(password, src, 1000);
byte[] inArray = provider.GetBytes(20/*bytes like in SHA-1*/);
return Convert.ToBase64String(inArray);
}
或检查 1.4 版本可用的其他 API here