Class: Argon2::Password

Inherits:
Object
  • Object
show all
Defined in:
lib/argon2.rb

Overview

Front-end API for the Argon2 module.

Constant Summary collapse

DEFAULT_T_COST =

Expose constants for the options supported and default used for passwords.

Argon2::Profiles::RFC_9106_LOW_MEMORY[:t_cost]
DEFAULT_M_COST =
Argon2::Profiles::RFC_9106_LOW_MEMORY[:m_cost]
DEFAULT_P_COST =
Argon2::Profiles::RFC_9106_LOW_MEMORY[:p_cost]
MIN_T_COST =
1
MAX_T_COST =
750
MIN_M_COST =
3
MAX_M_COST =
31
MIN_P_COST =
1
MAX_P_COST =
8

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Password

Returns a new instance of Password.



25
26
27
28
29
30
31
32
# File 'lib/argon2.rb', line 25

def initialize(options = {})
  options.update(Profiles[options[:profile]]) if options.key?(:profile)

  init_costs(options)

  @salt_do_not_supply = options[:salt_do_not_supply]
  @secret = options[:secret]
end

Class Method Details

.create(pass, options = {}) ⇒ Object

Helper class, just creates defaults and calls hash()



46
47
48
49
# File 'lib/argon2.rb', line 46

def self.create(pass, options = {})
  argon2 = Argon2::Password.new(options)
  argon2.create(pass)
end

.valid_hash?(hash) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/argon2.rb', line 51

def self.valid_hash?(hash)
  Argon2::HashFormat.valid_hash?(hash)
end

.verify_password(pass, hash, secret = nil) ⇒ Object

Raises:



55
56
57
58
59
# File 'lib/argon2.rb', line 55

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

Raises:



34
35
36
37
38
39
40
41
42
43
# File 'lib/argon2.rb', line 34

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