【发布时间】:2013-02-01 16:42:35
【问题描述】:
http://www.128bitstudios.com/2011/11/21/authentication-with-sinatra/
简单而漂亮的 Sinatra BCrypt 身份验证系统 - 我将不胜感激 =)
我发现这篇非常好的文章是关于使用 BCrypt 为 Sinatra 制作的一个非常简单的身份验证系统,我认为它很棒而且很简单,并带有很好的延续经典代码。
但是,我很难理解它,是的,我是菜鸟。如果你们中的一些人可以向我解释至少一些代码,我将非常感激,我特别感兴趣的是这部分
post "/signup" do
password_salt = BCrypt::Engine.generate_salt
password_hash = BCrypt::Engine.hash_secret(params[:password], password_salt)
#ideally this would be saved into a database, hash used just for sample
userTable[params[:username]] = {
:salt => password_salt,
:passwordhash => password_hash
}
session[:username] = params[:username]
redirect "/"
end
post "/login" do
if userTable.has_key?(params[:username])
user = userTable[params[:username]]
if user[:passwordhash] == BCrypt::Engine.hash_secret(params[:password], user[:salt])
session[:username] = params[:username]
redirect "/"
end
end
haml :error
end
我想代码没有任何问题,因为它是由比我更擅长这类东西的人编写的,但它可能包含错误,但很可能不会。由于我对 Sinatra 和 BCrypt 的使用都很陌生,如果有人能解释该过程及其加密密码的方式,我将不胜感激。
当你访问链接时,所有其余的代码都在那里,没有必要在这里全部粘贴。
此外,我认为如果我正确理解 BCryptEngine 从用户参数创建盐:密码,但我不明白它如何将用户保存到表和所有其他东西。谢谢:)
【问题讨论】:
标签: authentication encryption sinatra haml bcrypt-ruby