module Sinatra::Authentication::Cookies::Hashing

Public Instance Methods

check?(user_id, ip, key, crypted) click to toggle source
# File lib/sinatra/authentication/cookies.rb, line 18
def check?(user_id, ip, key, crypted)
    hash, salt = unserialize(crypted)
    self.hash(user_id, ip, key, salt) == hash
end
decrypt(crypted, key) click to toggle source
# File lib/sinatra/authentication/cookies.rb, line 14
def decrypt(crypted, key)
    Encryptor.decrypt(crypted, :key => key)
end
encrypt(user_id, ip, key, salt = self.generate_salt) click to toggle source
# File lib/sinatra/authentication/cookies.rb, line 9
def encrypt(user_id, ip, key, salt = self.generate_salt)
    hash = serialize(hash(user_id, ip, key, salt), salt)
    { :hash => hash, :salt => salt }
end

Protected Instance Methods

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

This method can be customized

# File lib/sinatra/authentication/cookies.rb, line 31
def hash(user_id, ip, key, salt)
    Digest::SHA512.hexdigest("#{ user_id }:#{ ip }:#{ key }:#{ salt }")
end
serialize(hash, salt) click to toggle source
# File lib/sinatra/authentication/cookies.rb, line 35
def serialize(hash, salt)
    hash + salt
end
unserialize(serialized) click to toggle source
# File lib/sinatra/authentication/cookies.rb, line 39
def unserialize(serialized)
    return serialized[0..127], serialized[128..-1]
end