Example Auth Lib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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