Skip to content

Instantly share code, notes, and snippets.

@soimafreak soimafreak/auth.rb
Created Oct 23, 2013

Embed
What would you like to do?
Example Auth Lib
#
# Simple Authentication library
#
require 'digest'
require 'securerandom'
class Auth
def initialize(opts)
@salt_length=opts[:salt_length] || 32
end
def self.gen_salt()
return SecureRandom.hex(@salt_length)
end
def self.gen_hash(salt,shake)
return "sha256$#{salt}$" + Digest::SHA256.hexdigest(salt+shake)
end
def self.get_salt(hash)
salt = hash.split('$')[1]
return salt
end
def self.get_algo(hash)
algo = hash.split('$')[0]
return algo
end
def self.get_hash(hash)
hash = hash.split('$')[3]
return hash
end
def self.hash_ok?(dbhash,shake)
algo =self.get_algo(dbhash)
salt = self.get_salt(dbhash)
newhash = self.gen_hash(salt,shake)
if newhash == dbhash
return true
else
return false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.