Skip to content

Commit

Permalink
Expose the default costs used through constants (#57)
Browse files Browse the repository at this point in the history
This allows third-party libraries to inspect those values and
use them.
  • Loading branch information
guilleiguaran committed Apr 11, 2023
1 parent 8d2c132 commit d62ecf8
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions lib/argon2.rb
Expand Up @@ -10,15 +10,26 @@
module Argon2
# Front-end API for the Argon2 module.
class Password
# Expose constants for the options supported and default used for passwords.
DEFAULT_T_COST = 2
DEFAULT_M_COST = 16
DEFAULT_P_COST = 1
MIN_T_COST = 1
MAX_T_COST = 750
MIN_M_COST = 1
MAX_M_COST = 31
MIN_P_COST = 1
MAX_P_COST = 8

def initialize(options = {})
@t_cost = options[:t_cost] || 2
raise ArgonHashFail, "Invalid t_cost" if @t_cost < 1 || @t_cost > 750
@t_cost = options[:t_cost] || DEFAULT_T_COST
raise ArgonHashFail, "Invalid t_cost" if @t_cost < MIN_T_COST || @t_cost > MAX_T_COST

@m_cost = options[:m_cost] || 16
raise ArgonHashFail, "Invalid m_cost" if @m_cost < 1 || @m_cost > 31
@m_cost = options[:m_cost] || DEFAULT_M_COST
raise ArgonHashFail, "Invalid m_cost" if @m_cost < MIN_M_COST || @m_cost > MAX_M_COST

@p_cost = options[:p_cost] || 1
raise ArgonHashFail, "Invalid p_cost" if @p_cost < 1 || @p_cost > 8
@p_cost = options[:p_cost] || DEFAULT_P_COST
raise ArgonHashFail, "Invalid p_cost" if @p_cost < MIN_P_COST || @p_cost > MAX_P_COST

@salt_do_not_supply = options[:salt_do_not_supply]
@secret = options[:secret]
Expand Down

0 comments on commit d62ecf8

Please sign in to comment.