module Argon2::Simple

Argon2::Simple

Public Class Methods

check(pw_clear, pw_hashed) click to toggle source

Accepts a clear password and a hashed value. Returns true if the clear password matches the hash. Does not throw any exceptions if the hash is not a valid Argon2 hash.

ok = Argon2::Simple.check(pw_clear, hashed)
# File lib/argon2/simple.rb, line 49
def self.check(pw_clear, pw_hashed)
        # must have both values as strings
        pw_clear.is_a?(String) or return false
        pw_hashed.is_a?(String) or return false
        
        # check cache
        if @@cache
                if acceptables = @@cache[pw_hashed]
                        if acceptables[pw_clear]
                                return true
                        end
                end
        end
        
        # It wasn't in the cache, so check the hard way.
        # NOTE: Argon2 crashes if the string being checked isn't a valid hash.
        # That seems stupid to me, because if it's not a valid hash then
        # it's not the right password. But whatever. We handle the exception
        # quietly here by just returning false.
        begin
                if Argon2::Password.verify_password(pw_clear, pw_hashed)
                        if @@cache
                                @@cache[pw_hashed] ||= LruRedux::Cache.new(10)
                                @@cache[pw_hashed][pw_clear] = true
                        end
                        return true
                else
                        return false
                end
        rescue
                return false
        end
end
hash(pw_clear) click to toggle source

Accepts a clear password and returns its hashed value.

hashed = Argon2::Simple.hash(pw_clear)
# File lib/argon2/simple.rb, line 38
def self.hash(pw_clear)
        return Argon2::Password.new.create(pw_clear)
end
reset(max=100) click to toggle source

Resets the cache. By default sets the cache limit to 100:

Argon2::Simple.reset

The optional parameter sets the cache to the given number. So to set it to 1000:

Argon2::Simple.reset 1000

To have no cache, set the max to 0:

Argon2::Simple.reset 0
# File lib/argon2/simple.rb, line 23
def self.reset(max=100)
        if max > 0
                @@cache = LruRedux::Cache.new(max)
        else
                @@cache = nil
        end
end