Class: Argon2::Password

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

Overview

Front-end API for the Argon2 module.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Password

Returns a new instance of Password.

Raises:



12
13
14
15
16
17
18
19
# File 'lib/argon2.rb', line 12

def initialize(options = {})
  @t_cost = options[:t_cost] || 2
  raise ArgonHashFail, "Invalid t_cost" if @t_cost < 1 || @t_cost > 750
  @m_cost = options[:m_cost] || 16
  raise ArgonHashFail, "Invalid m_cost" if @m_cost < 1 || @m_cost > 31
  @salt = options[:salt_do_not_supply] || Engine.saltgen
  @secret = options[:secret]
end

Class Method Details

.create(pass) ⇒ Object

Helper class, just creates defaults and calls hash()



30
31
32
33
# File 'lib/argon2.rb', line 30

def self.create(pass)
  argon2 = Argon2::Password.new
  argon2.create(pass)
end

.valid_hash?(hash) ⇒ Boolean

Supports 1 and argon2id formats.

Returns:

  • (Boolean)


36
37
38
# File 'lib/argon2.rb', line 36

def self.valid_hash?(hash)
  /^\$argon2(id?|d).{,113}/ =~ hash
end

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

Raises:



40
41
42
43
44
# File 'lib/argon2.rb', line 40

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:



21
22
23
24
25
26
27
# File 'lib/argon2.rb', line 21

def create(pass)
  raise ArgonHashFail, "Invalid password (expected string)" unless
    pass.is_a?(String)

  Argon2::Engine.hash_argon2id_encode(
          pass, @salt, @t_cost, @m_cost, @secret)
end