如何实现 salt 以防止对密码进行预计算字典攻击 [英] how salt can be implemented to prevent pre-computation dictionary attack on password

查看:122
本文介绍了如何实现 salt 以防止对密码进行预计算字典攻击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

盐使每个用户的密码散列都是唯一的,并在散列之前向密码添加盐以防止字典攻击.但是如何?

A salt makes every users password hash unique, and adding a salt to a password before hashing to protect against a dictionary attack. But how?

推荐答案

您几乎肯定想要的工具称为 PBKDF2(基于密码的密钥派生函数 2).它广泛可用,名称为pbkdf2"或RFC 2898".PBKDF2 提供加盐(使两个其他相同的密码不同)和拉伸(使猜测密码成本高昂).

The tool you almost certainly want is called PBKDF2 (Password-Based Key Derivation Function 2). It's widely available, either under the name "pbkdf2" or "RFC 2898". PBKDF2 provides both salting (making two otherwise identical passwords different) and stretching (making it expensive to guess passwords).

无论您为什么系统开发,都可能有一个函数可以接受密码、salt、迭代次数和输出大小.鉴于这些,它将输出一些字节串.根据您的情况,有几种方法可以实际使用它(最明显的是您是处理本地身份验证还是远程身份验证?)

Whatever system you are developing for probably has a function available that takes a password, a salt, a number of iterations, and an output size. Given those, it will output some string of bytes. There are several ways to actually make use of this depending on your situation (most notably are you dealing with local authentication or remote authentication?)

大多数人都在寻找远程身份验证,所以让我们通过一种合理的方式来实现,使用确定性和随机盐的组合.(请参阅下面与@SilverlightFox 的进一步讨论.)

Most people are looking for remote authentication, so let's walk through a reasonable way to implement that using a mix of deterministic and random salts. (See further discussion below w/ @SilverlightFox.)

一、高级方法:

  • 针对确定性盐在客户端上进行散列.客户端永远不应该向服务器发送裸密码.用户一直重复使用他们的密码.您不想知道他们的实际密码.你宁愿永远看不到它.
  • 在服务器上随机加盐并拉伸,然后进行比较.

这是实际的细分:

  • 为您的盐选择特定于应用的组件.例如,net.robnapier.mygreatapp"可能是我的前缀.
  • 为您的盐选择用户特定的组件.此处通常使用用户 ID.
  • 连接它们以创建盐.例如,我的盐可能是net.robnapier.mygreatapp:suejones@example.org".实际的盐并不重要.重要的是,它至少在您的所有用户和所有其他网站中至少大部分"是独一无二的,这些网站也可能对您的用户的密码进行哈希处理.我给出的方案实现了这一点.
  • 为 PBKDF2 选择本地迭代次数.这个数字几乎可以肯定是 1000.这太少了迭代,但几乎所有 JavaScript 都可以合理处理.迭代次数越多,系统越安全,但性能越差.这是一种压力.
  • 为您的哈希选择一个长度.32 字节通常是一个不错的选择.
  • 如果您的系统允许您选择一个PRF",请选择一个.HMAC-SHA-256 是一个不错的选择.

您现在已准备好所有基本部件.让我们计算一些哈希值.

You now have all the basic pieces in place. Let's compute some hashes.

  • 在客户端上,使用上述设置获取密码并通过 PBKDF2.这将为您提供 32 个字节发送到服务器.
  • 在服务器上,如果这是帐户创建,请创建 8 或 16 字节的随机数据作为此帐户的盐.将其与用户名一起保存在数据库中.使用该盐和另一组迭代(如果您不在 Node 中,通常为 10,000 或 100,000)并将 PBKDF2 应用于用户发送的数据.将其存储在数据库中.如果您正在测试密码,只需从数据库中读取盐并重新应用 PBKDF2 进行验证.

我在这里说PBKDF2"的任何地方都有另一种选择,其中最常见的可能是 scrypt(也有 bcrypt).其他选项在技术上优于 PBKDF2.我认为没有人会不同意这一点.我通常推荐 PBKDF2,因为它无处不在,而且没有什么问题.但是,如果您有可用的 scrypt,请随意使用它.客户端和服务器不必使用相同的算法(客户端可以使用 PBKDF2,服务器可以根据需要使用 scrypt).

Everywhere I say "PBKDF2" here there are another options, probably the most common of which is scrypt (there is also bcrypt). The other options are technically better than PBKDF2. I don't think anyone would disagree with that. I usually recommend PBKDF2 because it's so ubiquitous and there's nothing really wrong with it. But if you have scrypt available, feel free to use that. The client and server do not have to use the same algorithm (the client can use PBKDF2 and the server can use scrypt if you like).

这篇关于如何实现 salt 以防止对密码进行预计算字典攻击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆