首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rfc2898 / PBKDF2以SHA256为文摘的c#

Rfc2898 / PBKDF2以SHA256为文摘的c#
EN

Stack Overflow用户
提问于 2013-09-06 00:34:55
回答 8查看 21.9K关注 0票数 25

我想在Rfc2898中使用c#派生密钥。我还需要使用SHA256作为Rfc2898的摘要。我找到了类Rfc2898DeriveBytes,但它使用的是SHA-1,我看不出有什么方法可以让它使用不同的摘要。

是否有一种方法可以在Rfc2898中使用c#中的SHA256作为摘要(但没有从头开始实现)?

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2013-09-06 03:20:07

看看布鲁诺·加西亚的答案。

在我开始回答时,Rfc2898DeriveBytes无法配置为使用不同的哈希函数。不过,在此期间,情况有所改善;参见布鲁诺·加西亚的回答。以下函数可用于生成用户提供的密码的散列版本,以存储在数据库中以进行身份验证。

对于旧的.NET框架的用户来说,这仍然是有用的:

代码语言:javascript
复制
// 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;
        }
    }
票数 15
EN

Stack Overflow用户

发布于 2017-10-28 16:50:36

.NET核心有一个新的Rfc2898DeriveBytes实现。

The CoreFX版本不再有硬编码的散列算法。

该代码可在Github上使用。。它于2017年3月被合并为master,并随.NET Core2.0一起发布。

票数 17
EN

Stack Overflow用户

发布于 2018-08-04 23:39:37

对于那些需要哈希算法的人,.NET Framework4.7.2包括一个允许指定散列算法的Rfc2898DeriveBytes过载

代码语言:javascript
复制
byte[] bytes;
using (var deriveBytes = new Rfc2898DeriveBytes(password, salt, iterations, HashAlgorithmName.SHA256))
{
    bytes = deriveBytes.GetBytes(PBKDF2SubkeyLength);
}

目前的HashAlgorithmName选项是:

  • MD5
  • SHA1
  • SHA256
  • SHA384
  • SHA512
票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18648084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档