使用 .NET Core 生成哈希
Generating Hashes using .NET Core
我需要生成随机唯一哈希值以用于 .Net Core 中的密码重置链接。在 ASP.NET Identity 中,我发现这很有用,我想知道 RNGCryptoServiceProvider 是否仍然确保生成的哈希值的随机性。
using (RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider())
{
var data = new byte[4];
for (int i = 0; i < 10; i++)
{
//filled with an array of random numbers
rngCsp.GetBytes(data);
//this is converted into a character from A to Z
var randomchar = Convert.ToChar(
//produce a random number
//between 0 and 25
BitConverter.ToUInt32(data, 0) % 26
//Convert.ToInt32('A')==65
+ 65
);
token.Append(randomchar);
}
}
使用 .net core 和使用 which 类 实现该目标的最佳方法是什么?
RNGCryptoServiceProvider
不在 .NET Core 中。使用 RandomNumberGenerator.Create()
获取可在正确平台上运行的 CSPRNG 实例。它的 API 与 RNGCryptoServiceProvider
相同,只是缺少 GetNonZeroBytes
(我认为这根本不应该存在)。
在 Windows 上,这将在 CNG 中归结为 BCryptGenRandom
,在 Linux 上,它将使用 OpenSSL。
我需要生成随机唯一哈希值以用于 .Net Core 中的密码重置链接。在 ASP.NET Identity 中,我发现这很有用,我想知道 RNGCryptoServiceProvider 是否仍然确保生成的哈希值的随机性。
using (RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider())
{
var data = new byte[4];
for (int i = 0; i < 10; i++)
{
//filled with an array of random numbers
rngCsp.GetBytes(data);
//this is converted into a character from A to Z
var randomchar = Convert.ToChar(
//produce a random number
//between 0 and 25
BitConverter.ToUInt32(data, 0) % 26
//Convert.ToInt32('A')==65
+ 65
);
token.Append(randomchar);
}
}
使用 .net core 和使用 which 类 实现该目标的最佳方法是什么?
RNGCryptoServiceProvider
不在 .NET Core 中。使用 RandomNumberGenerator.Create()
获取可在正确平台上运行的 CSPRNG 实例。它的 API 与 RNGCryptoServiceProvider
相同,只是缺少 GetNonZeroBytes
(我认为这根本不应该存在)。
在 Windows 上,这将在 CNG 中归结为 BCryptGenRandom
,在 Linux 上,它将使用 OpenSSL。