Class: Argon2::Password
- Inherits:
-
Object
- Object
- Argon2::Password
- Defined in:
- lib/argon2.rb
Overview
Front-end API for the Argon2 module.
Class Method Summary collapse
-
.create(pass, options = {}) ⇒ Object
Helper class, just creates defaults and calls hash().
- .valid_hash?(hash) ⇒ Boolean
- .verify_password(pass, hash, secret = nil) ⇒ Object
Instance Method Summary collapse
- #create(pass) ⇒ Object
-
#initialize(options = {}) ⇒ Password
constructor
A new instance of Password.
Constructor Details
#initialize(options = {}) ⇒ Password
Returns a new instance of Password.
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/argon2.rb', line 13 def initialize( = {}) @t_cost = [:t_cost] || 2 raise ArgonHashFail, "Invalid t_cost" if @t_cost < 1 || @t_cost > 750 @m_cost = [:m_cost] || 16 raise ArgonHashFail, "Invalid m_cost" if @m_cost < 1 || @m_cost > 31 @p_cost = [:p_cost] || 1 raise ArgonHashFail, "Invalid p_cost" if @p_cost < 1 || @p_cost > 8 @salt_do_not_supply = [:salt_do_not_supply] @secret = [:secret] end |
Class Method Details
.create(pass, options = {}) ⇒ Object
Helper class, just creates defaults and calls hash()
39 40 41 42 |
# File 'lib/argon2.rb', line 39 def self.create(pass, = {}) argon2 = Argon2::Password.new() argon2.create(pass) end |
.valid_hash?(hash) ⇒ Boolean
44 45 46 |
# File 'lib/argon2.rb', line 44 def self.valid_hash?(hash) Argon2::HashFormat.valid_hash?(hash) end |
.verify_password(pass, hash, secret = nil) ⇒ Object
48 49 50 51 52 |
# File 'lib/argon2.rb', line 48 def self.verify_password(pass, hash, secret = nil) raise ArgonHashFail, "Invalid hash" unless valid_hash?(hash) Argon2::Engine.argon2_verify(pass, hash, secret) end |
Instance Method Details
#create(pass) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/argon2.rb', line 27 def create(pass) raise ArgonHashFail, "Invalid password (expected string)" unless pass.is_a?(String) # Ensure salt is freshly generated unless it was intentionally supplied. salt = @salt_do_not_supply || Engine.saltgen Argon2::Engine.hash_argon2id_encode( pass, salt, @t_cost, @m_cost, @p_cost, @secret) end |