Module: Argon2::Simple
- Defined in:
- lib/argon2/simple.rb
Overview
Argon2::Simple
Constant Summary collapse
- @@cache =
nil
Class Method Summary collapse
-
.check(pw_clear, pw_hashed) ⇒ Object
Accepts a clear password and a hashed value.
-
.hash(pw_clear) ⇒ Object
Accepts a clear password and returns its hashed value.
-
.reset(max = 100) ⇒ Object
Resets the cache.
Class Method Details
.check(pw_clear, pw_hashed) ⇒ Object
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)
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# 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) ⇒ Object
Accepts a clear password and returns its hashed value.
hashed = Argon2::Simple.hash(pw_clear)
38 39 40 |
# File 'lib/argon2/simple.rb', line 38 def self.hash(pw_clear) return Argon2::Password.new.create(pw_clear) end |
.reset(max = 100) ⇒ Object
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
23 24 25 26 27 28 29 |
# 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 |