【发布时间】:2014-01-31 20:48:24
【问题描述】:
这是来自github页面:
require 'bcrypt'
class User < ActiveRecord::Base
# users.password_hash in the database is a :string
include BCrypt
def password
@password ||= Password.new(password_hash)
end
def password=(new_password)
@password = Password.create(new_password)
self.password_hash = @password
end
end
看来要访问密码方法,您需要从 create 方法中将其作为属性调用:
@user.password = user_params[:password]
@user.save
好的……好吗?但是盐现在存放在哪里?我完全不明白,这怎么可能再远程安全了?
要检索散列密码,您需要这个方法:
def password
@password ||= Password.new(password_hash)
end
并将其称为属性:
if @user.password == params[:password]
give_token
else
false
end
所以看起来一切都在没有盐的情况下工作......它是如何做到的?
这意味着我现在只需要数据库中的一列来处理密码,对吗?password 或 password_hash 而不是 password_salt | password_hash?
那么为什么github页面会这样说:
但即使这样也有弱点——攻击者可以运行列表 可能的密码通过相同的算法,将结果存储在 大数据库,然后通过哈希查找密码:
PrecomputedPassword.find_by_hash(<unique gibberish>).password #=> "secret1" Salts
然后这才是真正让我着迷的:
The solution to this is to add a small chunk of random data -- called a salt -- to the password before it's hashed:
如果 bcrypt 自动处理所有事情,他们为什么要解释所有这些?
hash(salt + p) #=> <really unique gibberish> The salt is then stored along with the hash in the database, and used to check potentially valid passwords: <really unique gibberish> =? hash(salt + just_entered_password) bcrypt-ruby automatically handles the storage and generation of these salts for you.
有人能解释一下 bcrypt 如何存储和生成这些盐吗?为什么它说它为我处理了这一切,然后继续告诉我如何生成盐?我需要在我的模型中运行这样的东西吗:self.password_hash = hash(salt + p)
Argh 太困惑了,我曾经完全得到盐和哈希,现在他们已经改变了这一切。 糟糕的、不清楚的文档...它们似乎向您展示了如何在没有盐的情况下使用 bcrypt 并提供大量示例,然后简要提及如何正确地使用盐在底部。
谁能给我一个例子,如何使用新版本的 bcrypt 生成盐和哈希,以及如何进行身份验证?
【问题讨论】:
-
不管你是否明白这一点,你应该停止你正在做的事情并使用
has_secure_password,它为你管理非常重要的存储任务密码安全。 -
Godammit 我很好地用 bcrypt 腌制和散列自己......为什么他们必须改变这一切?
-
我以为
has_secure_password使用了 bcrypt,但我可能错了 -
是的,我也这么认为,我只是说自己手动使用 bcrypt 的方法。不管怎样,我现在已经联系了
has_secure_password,一切都很好。
标签: ruby-on-rails bcrypt