Class: Argon2::HashFormat

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

Overview

Get the values from an Argon2 compatible string.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(digest) ⇒ HashFormat

FIXME: Reduce complexity/AbcSize rubocop:disable Metrics/AbcSize



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/argon2/hash_format.rb', line 12

def initialize(digest)
  digest = digest.to_s unless digest.is_a?(String)

  raise Argon2::ArgonHashFail, 'Invalid Argon2 hash' unless self.class.valid_hash?(digest)

  _, variant, version, config, salt, checksum = digest.split('$')
  # Regex magic to extract the values for each setting
  version = /v=(\d+)/.match(version)
  t_cost  = /t=(\d+),/.match(config)
  m_cost  = /m=(\d+),/.match(config)
  p_cost  = /p=(\d+)/.match(config)

  # Make sure none of the values are missing
  raise Argon2::ArgonHashFail, 'Invalid Argon2 version' if version.nil?
  raise Argon2::ArgonHashFail, 'Invalid Argon2 time cost' if t_cost.nil?
  raise Argon2::ArgonHashFail, 'Invalid Argon2 memory cost' if m_cost.nil?
  raise Argon2::ArgonHashFail, 'Invalid Argon2 parallelism cost' if p_cost.nil?

  @variant  = variant.to_str
  @version  = version[1].to_i
  @t_cost   = t_cost[1].to_i
  @m_cost   = m_cost[1].to_i
  @p_cost   = p_cost[1].to_i
  @salt     = salt.to_str
  @checksum = checksum.to_str
end

Instance Attribute Details

#checksumObject (readonly)

Returns the value of attribute checksum.



8
9
10
# File 'lib/argon2/hash_format.rb', line 8

def checksum
  @checksum
end

#m_costObject (readonly)

Returns the value of attribute m_cost.



8
9
10
# File 'lib/argon2/hash_format.rb', line 8

def m_cost
  @m_cost
end

#p_costObject (readonly)

Returns the value of attribute p_cost.



8
9
10
# File 'lib/argon2/hash_format.rb', line 8

def p_cost
  @p_cost
end

#saltObject (readonly)

Returns the value of attribute salt.



8
9
10
# File 'lib/argon2/hash_format.rb', line 8

def salt
  @salt
end

#t_costObject (readonly)

Returns the value of attribute t_cost.



8
9
10
# File 'lib/argon2/hash_format.rb', line 8

def t_cost
  @t_cost
end

#variantObject (readonly)

Returns the value of attribute variant.



8
9
10
# File 'lib/argon2/hash_format.rb', line 8

def variant
  @variant
end

#versionObject (readonly)

Returns the value of attribute version.



8
9
10
# File 'lib/argon2/hash_format.rb', line 8

def version
  @version
end

Class Method Details

.valid_hash?(digest) ⇒ Boolean

Checks whether a given digest is a valid Argon2 hash.

Supports 1 and argon2id formats.

Returns:

  • (Boolean)


45
46
47
# File 'lib/argon2/hash_format.rb', line 45

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