Skip to content

Instantly share code, notes, and snippets.

@soimafreak
Created October 23, 2013 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soimafreak/7127583 to your computer and use it in GitHub Desktop.
Save soimafreak/7127583 to your computer and use it in GitHub Desktop.
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