Advertisement
Guest User

Untitled

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