【问题标题】:What format is the output of HashPasswordForStoringInConfigFile () and how to store it the best?HashPasswordForStoringInConfigFile() 的输出是什么格式,如何存储最好?
【发布时间】:2009-04-11 18:29:41
【问题描述】:

我正在使用此函数为密码生成哈希,然后将其存储在数据库(SQL Server)中。

代码如下:

byte[] saltBytes = new byte[16];
new RNGCryptoServiceProvider ().GetBytes (saltBytes);
string salt = Convert.ToBase64String (saltBytes);
string saltedPasswordHash =
FormsAuthentication.HashPasswordForStoringInConfigFile (password + salt, FormsAuthPasswordFormat.SHA1.ToString ());

现在的问题是:HashPasswordForStoringInConfigFile () 的输出是什么格式 - 我将其存储为 char(length) 还是 nchar(length)?

或者是否有任何其他首选方式来存储哈希,也许不是作为字符串?

非常感谢任何输入和半相关的 cmets。

【问题讨论】:

标签: c# hash types passwords


【解决方案1】:

SQL Server 支持旨在存储二进制数据的二进制列(binary(len)varbinary(len))。您可能要考虑使用它们。在这种情况下,您可以直接使用 System.Security.Cryptography.SHA512Managed 等类,而不是 HashPasswordForStoringInConfigFile

【讨论】:

    【解决方案2】:

    由于您使用的是 C#,因此可能值得考虑使用 BCrypt 哈希解决方案。它是由编写 OpenBSD 的人在 BCrypt 加密算法上设计的,是一个非常强大的算法。最好的部分是您不必担心盐(但它们就在那里),并且随着时间的推移您可以使盐生成变得困难。

    BCrypt.net - Strong Password Hashing for .NET and Mono

    【讨论】:

    • BCrypt 是存储密码哈希的好方法,如果您使用足够高的工作因子。 PBKDF2 和 Scrypt 是另外两个当前的竞争者,也具有足够高的迭代次数/工作因子。在所有情况下,@LeakyCode 都是正确的 - 用于 salt 和 hash 的 BINARY(len) 列都是最有效的。
    猜你喜欢
    • 1970-01-01
    • 2021-10-03
    • 2010-11-17
    • 1970-01-01
    • 2013-04-15
    • 2014-12-28
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多