【发布时间】:2017-07-23 12:19:48
【问题描述】:
Web 服务将用户信息存储在数据库中并使用密码进行身份验证。下面是在 ruby 中实现用户密码存储和认证的方式(实际的数据存储和检索超出了示例的范围):
require 'digest'
class User
# Use salted passwords
PASSWORD_SALT="trustno1"
# Stored password hash will be accessible through user.hashed_password
attr_accessor :hashed_password
# Authenticates user against given password and returns true
# the password matches the stored one
def verify_password(password)
if hashed_password.nil? || password.nil?
false
else
User.hash_password(password) == hashed_password
end
end
# Changes user's password
def change_password(new_password)
self.hashed_password = User.hash_password(new_password.to_s)
end
# Hashes the input with salt
def self.hash_password(password)
Digest::MD5.hexdigest(password + PASSWORD_SALT)
end
end
但是,我被告知我遇到了与密码安全相关的问题,但我找不到任何问题。
【问题讨论】:
-
(1) MD5 不适合密码散列(它坏了而且太快了)(2) 你应该为每个密码使用一个新的随机盐 (3) 你不应该尝试在你的密码上实现安全的密码存储拥有但使用现有的(经过验证且安全的)解决方案,例如 Rails 的内置
has_secure_password选项。
标签: ruby-on-rails ruby authentication passwords