简体   繁体   English

Rails:设计gem密码加密

[英]Rails: Devise gem password encryption

I am working on a feature, in which I must update all the account details in the database manually. 我正在开发一项功能,在该功能中,我必须手动更新数据库中的所有帐户详细信息。 It is working fine but the only issue is, Since we have also using the devise gem for the login feature. 它工作正常,但唯一的问题是,由于我们还将devise gem用于登录功能。 I must store the encrypted format of the password I am updating manually. 我必须存储要手动更新的密码的加密格式。 So I just want to know how the devise gem encrypts a password. 所以我只想知道devise gem如何加密密码。

If my password is "password". 如果我的密码是“密码”。 I must manually encrypt this and store it in the database in the same way of the devise. 我必须手动进行加密,并以与设计相同的方式将其存储在数据库中。

Please revert back for the help asap. 请尽快返回以获取帮助。

I'm assuming you have a list of usernames/passwords which you'd like to mass-assign into a database for use in an app that authenticates via Devise. 我假设您有一个用户名/密码列表,您希望将这些用户名/密码批量分配到数据库中,以便在通过Devise进行身份验证的应用程序中使用。

Devise by default uses BCrypt, (there is a gem) 默认情况下,Devise使用BCrypt,(有一个gem)

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

You can create instances of the users in your list and save to the database in a Rake task, for example. 例如,您可以在列表中创建用户实例,然后在Rake任务中将其保存到数据库。

@user = User.new
@user.username = "foobar"
@user.password = "password"
@user.save

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM