我想在Rfc2898中使用c#派生密钥。我还需要使用SHA256作为Rfc2898的摘要。我找到了类Rfc2898DeriveBytes,但它使用的是SHA-1,我看不出有什么方法可以让它使用不同的摘要。
是否有一种方法可以在Rfc2898中使用c#中的SHA256作为摘要(但没有从头开始实现)?
发布于 2013-09-06 03:20:07
看看布鲁诺·加西亚的答案。
在我开始回答时,Rfc2898DeriveBytes无法配置为使用不同的哈希函数。不过,在此期间,情况有所改善;参见布鲁诺·加西亚的回答。以下函数可用于生成用户提供的密码的散列版本,以存储在数据库中以进行身份验证。
对于旧的.NET框架的用户来说,这仍然是有用的:
// NOTE: The iteration count should
// be as high as possible without causing
// unreasonable delay. Note also that the password
// and salt are byte arrays, not strings. After use,
// the password and salt should be cleared (with Array.Clear)
public static byte[] PBKDF2Sha256GetBytes(int dklen, byte[] password, byte[] salt, int iterationCount){
using(var hmac=new System.Security.Cryptography.HMACSHA256(password)){
int hashLength=hmac.HashSize/8;
if((hmac.HashSize&7)!=0)
hashLength++;
int keyLength=dklen/hashLength;
if((long)dklen>(0xFFFFFFFFL*hashLength) || dklen<0)
throw new ArgumentOutOfRangeException("dklen");
if(dklen%hashLength!=0)
keyLength++;
byte[] extendedkey=new byte[salt.Length+4];
Buffer.BlockCopy(salt,0,extendedkey,0,salt.Length);
using(var ms=new System.IO.MemoryStream()){
for(int i=0;i<keyLength;i++){
extendedkey[salt.Length]=(byte)(((i+1)>>24)&0xFF);
extendedkey[salt.Length+1]=(byte)(((i+1)>>16)&0xFF);
extendedkey[salt.Length+2]=(byte)(((i+1)>>8)&0xFF);
extendedkey[salt.Length+3]=(byte)(((i+1))&0xFF);
byte[] u=hmac.ComputeHash(extendedkey);
Array.Clear(extendedkey,salt.Length,4);
byte[] f=u;
for(int j=1;j<iterationCount;j++){
u=hmac.ComputeHash(u);
for(int k=0;k<f.Length;k++){
f[k]^=u[k];
}
}
ms.Write(f,0,f.Length);
Array.Clear(u,0,u.Length);
Array.Clear(f,0,f.Length);
}
byte[] dk=new byte[dklen];
ms.Position=0;
ms.Read(dk,0,dklen);
ms.Position=0;
for(long i=0;i<ms.Length;i++){
ms.WriteByte(0);
}
Array.Clear(extendedkey,0,extendedkey.Length);
return dk;
}
}发布于 2017-10-28 16:50:36
.NET核心有一个新的Rfc2898DeriveBytes实现。
该代码可在Github上使用。。它于2017年3月被合并为master,并随.NET Core2.0一起发布。
发布于 2018-08-04 23:39:37
对于那些需要哈希算法的人,.NET Framework4.7.2包括一个允许指定散列算法的Rfc2898DeriveBytes过载:
byte[] bytes;
using (var deriveBytes = new Rfc2898DeriveBytes(password, salt, iterations, HashAlgorithmName.SHA256))
{
bytes = deriveBytes.GetBytes(PBKDF2SubkeyLength);
}目前的HashAlgorithmName选项是:
https://stackoverflow.com/questions/18648084
复制相似问题