module Sinatra::Authentication::Password::Hashing

Public Instance Methods

check?(password, crypted) click to toggle source
# File lib/sinatra/authentication/password.rb, line 27
def check?(password, crypted)
    hash, salt = unserialize(crypted)
    self.hash(password, salt) == hash
end
encrypt(password, salt = self.generate_salt) click to toggle source
# File lib/sinatra/authentication/password.rb, line 23
def encrypt(password, salt = self.generate_salt)
    serialize(hash(password, salt), salt)
end

Protected Instance Methods

generate_salt() click to toggle source
# File lib/sinatra/authentication/password.rb, line 33
def generate_salt
    salt = ""
    64.times { salt << (i = Kernel.rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61))).chr }
    salt
end
hash(password, salt) click to toggle source

This method can be customized

# File lib/sinatra/authentication/password.rb, line 40
def hash(password, salt)
    Digest::SHA512.hexdigest("#{ password }:#{ salt }")
end
serialize(hash, salt) click to toggle source
# File lib/sinatra/authentication/password.rb, line 44
def serialize(hash, salt)
    hash + salt
end
unserialize(serialized) click to toggle source
# File lib/sinatra/authentication/password.rb, line 48
def unserialize(serialized)
    return serialized[0..127], serialized[128..-1]
end