bcrypt-ruby : secure password

 

ruby 1.9.3 + rails 3.2.13

 

1. 修改Gemfile,添加bcrypt-ruby,gem 'bcrypt-ruby', '3.0.1'

 

2. 执行bundle install

 

3. rails g modle User name:string password_digest:string

 

4. rake db:migrate

 

5. 在app/modles/user.rb添加 has_secure_password,以及虚拟属性 password, password_confirmation

 

class User < ActiveRecord::Base
  attr_accessible :email, :name, :password, :password_confirmation
  has_secure_password


  before_save { |user| user.email = email.downcase }

  validates :password, presence: true, length: { minimum: 6 }
  validates :password_confirmation, presence: true
end

rails c

user = User.new(name: "zhangsan", email: "[email protected]", password: "123456", password_confirmation: "123456")

 

=> #<User id: nil, name: "zhangsan", email: "[email protected]", created_at: nil,

 

 updated_at: nil, password_digest: "$2a$10$pzxgXUT2/O2EkF1hY3J0juFK6vfz1BZE9AAxG

 

rdTselI...">

 

user.authenticate("123456")

 

=> #<User id: nil, name: "zhangsan", email: "[email protected]", created_at: nil,

 

 updated_at: nil, password_digest: "$2a$10$pzxgXUT2/O2EkF1hY3J0juFK6vfz1BZE9AAxG

 

rdTselI...">

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(password,bcrypt-ruby,password_digest)