使用salt安全密码哈希,但是如何将其存储在本地cookie中呢?

时间:2013-01-07 00:30:00

标签: c# asp.net passwords cryptography

我最近阅读了一篇有关使用“salt”安全散列用户密码的有趣文章。 (这是original article,遗憾的是,这篇文章时似乎已经关闭,所以here's缓存版本。)

我完全同意这个概念,除了我似乎无法找到一种在本地cookie(或会话)中安全存储用户登录的方法,因为salt + PBKDF2哈希组合每次都是随机完成的。为了更好地理解我的意思,让我从the article复制C#代码:

using System;
using System.Text;
using System.Security.Cryptography;

namespace PasswordHash
{
    /// <summary>
    /// Salted password hashing with PBKDF2-SHA1.
    /// Author: havoc AT defuse.ca
    /// www: http://crackstation.net/hashing-security.htm
    /// Compatibility: .NET 3.0 and later.
    /// </summary>
    class PasswordHash
    {
        // The following constants may be changed without breaking existing hashes.
        public const int SALT_BYTES = 24;
        public const int HASH_BYTES = 24;
        public const int PBKDF2_ITERATIONS = 1000;

        public const int ITERATION_INDEX = 0;
        public const int SALT_INDEX = 1;
        public const int PBKDF2_INDEX = 2;

        /// <summary>
        /// Creates a salted PBKDF2 hash of the password.
        /// </summary>
        /// <param name="password">The password to hash.</param>
        /// <returns>The hash of the password.</returns>
        public static string CreateHash(string password)
        {
            // Generate a random salt
            RNGCryptoServiceProvider csprng = new RNGCryptoServiceProvider();
            byte[] salt = new byte[SALT_BYTES];
            csprng.GetBytes(salt);

            // Hash the password and encode the parameters
            byte[] hash = PBKDF2(password, salt, PBKDF2_ITERATIONS, HASH_BYTES);
            return PBKDF2_ITERATIONS + ":" +
                Convert.ToBase64String(salt) + ":" +
                Convert.ToBase64String(hash);
        }

        /// <summary>
        /// Validates a password given a hash of the correct one.
        /// </summary>
        /// <param name="password">The password to check.</param>
        /// <param name="goodHash">A hash of the correct password.</param>
        /// <returns>True if the password is correct. False otherwise.</returns>
        public static bool ValidatePassword(string password, string goodHash)
        {
            // Extract the parameters from the hash
            char[] delimiter = { ':' };
            string[] split = goodHash.Split(delimiter);
            int iterations = Int32.Parse(split[ITERATION_INDEX]);
            byte[] salt = Convert.FromBase64String(split[SALT_INDEX]);
            byte[] hash = Convert.FromBase64String(split[PBKDF2_INDEX]);

            byte[] testHash = PBKDF2(password, salt, iterations, hash.Length);
            return SlowEquals(hash, testHash);
        }

        /// <summary>
        /// Compares two byte arrays in length-constant time. This comparison
        /// method is used so that password hashes cannot be extracted from
        /// on-line systems using a timing attack and then attacked off-line.
        /// </summary>
        /// <param name="a">The first byte array.</param>
        /// <param name="b">The second byte array.</param>
        /// <returns>True if both byte arrays are equal. False otherwise.</returns>
        private static bool SlowEquals(byte[] a, byte[] b)
        {
            uint diff = (uint)a.Length ^ (uint)b.Length;
            for (int i = 0; i < a.Length && i < b.Length; i++)
                diff |= (uint)(a[i] ^ b[i]);
            return diff == 0;
        }

        /// <summary>
        /// Computes the PBKDF2-SHA1 hash of a password.
        /// </summary>
        /// <param name="password">The password to hash.</param>
        /// <param name="salt">The salt.</param>
        /// <param name="iterations">The PBKDF2 iteration count.</param>
        /// <param name="outputBytes">The length of the hash to generate, in bytes.</param>
        /// <returns>A hash of the password.</returns>
        private static byte[] PBKDF2(string password, byte[] salt, int iterations, int outputBytes)
        {
            Rfc2898DeriveBytes pbkdf2 = new Rfc2898DeriveBytes(password, salt);
            pbkdf2.IterationCount = iterations;
            return pbkdf2.GetBytes(outputBytes);
        }
    }
}

您可以看到,验证密码的唯一方法是使用纯文本密码调用ValidatePassword。在我以前的普通SHA1实现中,为了在本地浏览器中存储用户登录,我将该SHA1值放在cookie中,并将其与存储在服务器上的数据库中的每个页面进行比较。但是你如何用这种“安全”方法做同样的事情?

有什么想法吗?

3 个答案:

答案 0 :(得分:3)

您不希望在Cookie中存储哈希密码,因为这与将密码本身存储在Cookie中是一回事。如果您只需要登录哈希,那么它就是密码。您希望使用随机盐散列用户密码的原因不是为了保护登录过程,而是为了保护您的密码表。如果攻击者窃取您的密码表,并且每个密码都没有使用唯一的盐进行哈希处理,那么他/她很容易找出许多密码。始终使用唯一的盐哈希用户的密码。用散列密码存储这个盐是很好的。如果您想要一种安全的方式来使用哈希来根据cookie中的数据对您的用户进行身份验证,您需要转向临时凭证或会话。我能想到的最简单的东西如下:

  1. 当您的用户使用其密码登录时,请创建“会话”。分配一个值以唯一标识此会话,存储创建会话的时间(以毫秒为单位),并创建一个随机值作为salt。

  2. 使用salt哈希会话的id。将此哈希和会话ID保存在用户的cookie中。

  3. 每次请求页面时,再次执行哈希并将其与存储在用户cookie中的值进行比较。如果值匹配并且自创建会话以来未过多时间,则您的用户可以查看该页面。否则,请使用密码再次登录。

答案 1 :(得分:1)

Hash存储在服务器上,作为用户身份验证的一部分,当明文密码发送到服务器时,计算出的盐(每个用户)存储在一个安全的数据库中并添加到密码中,并根据密码计算加密结果+哈希。

Cookie是错误的方法,因为它们会过期,并且没有理由说Web客户端需要盐。

Salting Your Password: Best Practices?

答案 2 :(得分:0)

哈希函数的盐通常是基于规则计算的。例如,您使用用户标识符作为salt本身。您使用“username”值在数据库中查找用户,然后使用用户ID作为salt。这样,您就不必将盐存储在任何位置,并且每个散列值的盐都不同。