使用盐来保护密码散列,但是如何将其存储在本地cookie中呢?

本文关键字:存储 中呢 cookie 保护 密码 | 更新日期: 2023-09-27 17:50:48

我最近读了一篇关于使用"salt"安全地散列用户密码的有趣文章。(这是最初的文章,不幸的是,在这篇文章发表的时候,它似乎已经关闭了,所以这里是缓存版本。)

我完全同意这个概念,除了我似乎找不到一种安全地将用户登录存储在本地cookie(或会话)中的方法,因为salt + PBKDF2哈希组合每次都是随机完成的。为了更好地理解我的意思,让我从文章中复制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中,并将其与存储在服务器数据库中的每个页面的SHA1值进行比较。但是如何使用这种"安全"的方法呢?

任何想法?

使用盐来保护密码散列,但是如何将其存储在本地cookie中呢?

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

  1. 当你的用户用他的密码登录时,创建一个'Session'。指定一个值来唯一标识这个会话,存储会话创建的时间(以毫秒为单位),并创建一个随机值作为salt。

  2. 用盐对会话id进行散列。

  3. 将此哈希值和会话id保存在用户的cookie中。
  4. 每次请求页面时,再次执行哈希并将其与存储在用户cookie中的值进行比较。如果值匹配并且自会话创建以来没有经过太多时间,则用户可以查看该页面。

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

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

设置密码:最佳做法?

散列函数的盐通常是基于规则计算的东西。例如,使用用户标识符作为盐本身。您使用"username"值在数据库中查找用户,然后使用用户id作为盐。这样,您就不必将盐存储在任何地方,并且每个散列值的盐是不同的。