【问题标题】:How to validate inputs with CryptSharp?如何使用 CryptSharp 验证输入?
【发布时间】:2016-09-13 14:28:37
【问题描述】:

使用以下函数,您可以使用 bcrypt 加密输入字符串。

public static string CreatePassword(string password)
{
    // no need to provide a Salt value since bcrypt does that automatically
    byte[] PasswordBytes = Encoding.ASCII.GetBytes(password);

    return Crypter.Blowfish.Crypt(PasswordBytes);
}

这使用了CryptSharp,这很棒,但是如何根据此函数返回的哈希验证用户输入?

我在库中找不到执行此操作的任何函数。

我能想到的最好方法是:

public static bool ValidatePassword(string password, string passwordHash)
{
    // crypt the entered password
    string Crypted = Crypter.Blowfish.Crypt(Encoding.ASCII.GetBytes(password));

    // compare the crypted password against the value in the database
    if (String.Compare(Crypted, passwordHash, false) != 0) return false;

    return true;
}

唯一的问题是盐值不一样,因此值几乎总是不一致。

【问题讨论】:

  • 你为什么不使用相同的盐来输入和存储密码

标签: c# cryptsharp


【解决方案1】:

盐应该是唯一的。以避免数据库密码破解相同的密码。您应该使用密码存储盐,如果用户登录,您应该使用相同的盐检查用户输入和密码

在第二个参数中,您可以提供自定义盐

 string salt = Crypter.Blowfish.GenerateSalt(20);
 Crypter.Blowfish.Crypt(PasswordBytes,salt);

验证你可以使用这个

public static bool ValidatePassword(string inputPassword, string storedPassword, string salt)
        {
            // crypt the entered password and stored password
            string CryptedInput = Crypter.Blowfish.Crypt(Encoding.ASCII.GetBytes(inputPassword), salt);
            string CryptedPassword = Crypter.Blowfish.Crypt(Encoding.ASCII.GetBytes(storedPassword), salt);

            // compare the crypted passwords
            return string.Equals(CryptedInput, CryptedPassword);
        }

【讨论】:

  • 或使用var salt = Crypter.Blowfish.GenerateSalt(6)
  • 认为 bcrypt 会生成自己的盐值吗?无论如何,这解决了 salt 问题,但是如何根据存储的内容验证输入的密码呢?
  • 我知道我回来晚了,但我不明白的是,如果我在登录时生成新的盐,那么新的哈希显然会不同于储存的盐,那么我怎么知道从哪里得到储存的盐呢?
  • 为什么要在登录时更改盐
  • 盐的目的是使每个散列唯一。如果某些用户具有相同的密码哈希值不一样,因为您使用了盐。 Withs 意味着你用一些随机文本对密码进行了哈希处理。盐可以是用户名或电子邮件,但您也可以使用一些随机文本。它还避免了人们使用彩虹桌。那是一个带有解密哈希的数据库。因为您的密码中添加了一些随机文本(盐)。哈希与彩虹表中的哈希不匹配。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-16
  • 1970-01-01
相关资源
最近更新 更多