這是從GitHub的頁面:bcrypt與salt發生了什麼?
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
看來,訪問密碼的方法,你需要調用它作爲從屬性創建方法:
@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
所以它似乎一切都沒有工作鹽......是怎麼做的做這個?
這意味着我現在只需要在我的數據庫中使用一列來處理passowords,對吧? 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)
呃很混亂我以前完全得到鹽和哈希值,現在他們已經改變了這一切。 可怕的,不清楚的文檔 ...他們似乎向你展示瞭如何在沒有鹽的情況下使用bcrypt並加載示例,然後簡要提及如何使用底部的鹽正確地做到這一點。
有人請給我一個例子如何使用新版本的bcrypt來生成鹽和哈希,以及如何進行身份驗證?
無論你摸不着頭腦,你應該停止你正在做什麼,並使用'has_secure_password',它爲您管理存儲密碼安全的*極不平凡的*任務。 – meagar
Godammit我很好,用bcrypt自己抨擊自己。爲什麼他們必須改變這一切? – Starkers
我以爲'has_secure_password'使用bcrypt,但我可能是錯的 – meagar