module SSHA

Public Class Methods

check_password(password, ssha) click to toggle source

Check the supplied password against the given hash and return true if they match, else false.

# File lib/ssha.rb, line 20
def self.check_password(password, ssha)
  #decoded = Base64.decode64(ssha[/{SSHA}(.*?)$/, 1])
  #hash = decoded[0,20] # isolate the hash
  salt = ssha[/\*(.+?)\*{SSHA}.+$/, 1] # isolate the salt
  self.hash_password(password, salt) == ssha
end
hash_password(password, salt=SSHA.new_salt) click to toggle source

hash the password using the given salt. If no salt is supplied, use a new one.

# File lib/ssha.rb, line 13
def self.hash_password(password, salt=SSHA.new_salt)
  '*' + salt.to_s + "*{SSHA}" + Base64.encode64("#{Digest::SHA1.digest("#{password}#{salt}")}#{salt}").chomp
end
new_salt(lenght=16, rand_opt=16, dim = 16) click to toggle source
# File lib/ssha.rb, line 6
def self.new_salt(lenght=16, rand_opt=16, dim = 16)
  lenght.times.inject('') {|t| t << rand(rand_opt).to_s(dim)}
end