Guest User

Untitled

a guest
Feb 20th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. require 'digest/sha2'
  2. class User < ActiveRecord::Base
  3. validates_uniqueness_of :username
  4.  
  5. def self.authenticate(username, password)
  6. user = User.find(:first, :conditions => "username = ?", username)
  7. if user.blank? || Digest::SHA256.hexdigest(password + user.password_salt) != user.password_hash
  8. raise "Username or password invalid"
  9. end
  10. end
  11.  
  12. def password=(pass)
  13. salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp
  14. self.password_salt, self.password_hash = salt, Digest::SHA256.hexdigest(pass + salt)
  15. end
  16.  
  17. end
Add Comment
Please, Sign In to add comment