小编Rud*_*udy的帖子

重点加强.我做得对吗?

我正在编写一个哈希密码的类,它通过使用System.Security.Cryptography.Rfc2898DeriveBytes类来生成用于计算哈希值的键来实现密钥拉伸.

代码基本上是这样的:

// Higher iterations value results in better key strength
var db = new Rfc2898DeriveBytes(password + salt + secretKeyFromConfigFile,
                                saltAsBytes,
                                iterations);

byte[] hashKey = db.GetBytes(64); // 64 bytes is the recommended size for the HMACSHA512 class

var sha512 = new System.Security.Cryptography.HMACSHA512(hashKey);

byte[] hashInput = System.Text.Encoding.UTF8.GetBytes(password + salt + secretKeyFromConfigFile);

byte[] hash = sha512.ComputeHash(hashInput);

return System.Convert.ToBase64String(hash);
Run Code Online (Sandbox Code Playgroud)

System.Security.Cryptography.Rfc2898DeriveBytes的文档表明它使用"...基于System.Security.Cryptography.HMACSHA1的伪随机数生成器"来派生密钥.

由于SHA512优于SHA1,所以简单地应用SHA512散列函数会更好吗?

byte[] hash;

for (int i = 0; i < iterations; i++)
{
    hash = sha512.ComputeHash(hash ?? hashInput);
}

return System.Convert.ToBase64String(hash); …
Run Code Online (Sandbox Code Playgroud)

c# security passwords cryptography

5
推荐指数
1
解决办法
2208
查看次数

点之前有空格可以接受吗?

以下第二种缩进方法的一般意见是什么?

// Normal indentation
a.Value        = "foobar";
ab.Checked     = false;
foo.Value      = "foobar";
foobar.Checked = true;

// Spaces before the dot to align the properties/methods
a     .Value   = "foobar";
ab    .Checked = false;
foo   .Value   = "foobar";
foobar.Checked = true;
Run Code Online (Sandbox Code Playgroud)

这应该是一个维基,但我要么没有足够的权限,要么不知道如何更改它.

编辑

我已经决定添加另一个例子来更好地展示这种缩进样式可能有用的地方.

fooA   .PropertyA = true;
foobarB.PropertyA = true;
Run Code Online (Sandbox Code Playgroud)

使用VS2010中的新多线编辑功能,可以更轻松地更改所有线路上的PropertyA.

在C#中也有空格,甚至在点之前的换行并不罕见(参见LINQ).

c# syntax coding-style

1
推荐指数
1
解决办法
204
查看次数

标签 统计

c# ×2

coding-style ×1

cryptography ×1

passwords ×1

security ×1

syntax ×1