Class: Altcha::Challenge

Inherits:
Object
  • Object
show all
Defined in:
lib/altcha-rails.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#algorithmObject

Returns the value of attribute algorithm.



49
50
51
# File 'lib/altcha-rails.rb', line 49

def algorithm
  @algorithm
end

#challengeObject

Returns the value of attribute challenge.



49
50
51
# File 'lib/altcha-rails.rb', line 49

def challenge
  @challenge
end

#max_numberObject

Returns the value of attribute max_number.



49
50
51
# File 'lib/altcha-rails.rb', line 49

def max_number
  @max_number
end

#saltObject

Returns the value of attribute salt.



49
50
51
# File 'lib/altcha-rails.rb', line 49

def salt
  @salt
end

#signatureObject

Returns the value of attribute signature.



49
50
51
# File 'lib/altcha-rails.rb', line 49

def signature
  @signature
end

Class Method Details

.create(algorithm: nil, hmac_key: nil, max_number: nil, expires: nil, number: nil) ⇒ Object

Raises:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/altcha-rails.rb', line 51

def self.create(algorithm: nil, hmac_key: nil, max_number: nil, expires: nil, number: nil)
  hmac_key ||= Altcha.hmac_key
  raise ConfigurationError, "Altcha.hmac_key is not set" if hmac_key.nil? || hmac_key.empty?

  algorithm  ||= Altcha.algorithm
  max_number ||= Altcha.max_number
  expires    ||= Time.now.to_i + Altcha.timeout.to_i
  number     ||= SecureRandom.random_number(max_number)

  ch = new
  ch.algorithm  = algorithm
  ch.max_number = max_number
  # Canonical v1 ALTCHA salt: random hex, expires parameter, trailing '&'
  # to delimit the parameter list from the nonce (CVE-2025-68113).
  ch.salt       = "#{SecureRandom.hex(12)}?expires=#{expires.to_i}&"
  ch.challenge  = Digest::SHA256.hexdigest(ch.salt + number.to_s)
  ch.signature  = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new(algorithm), hmac_key, ch.challenge)
  ch
end

Instance Method Details

#to_hObject



71
72
73
74
75
76
77
78
79
# File 'lib/altcha-rails.rb', line 71

def to_h
  {
    algorithm: algorithm,
    challenge: challenge,
    maxnumber: max_number,
    salt: salt,
    signature: signature,
  }
end

#to_json(*args) ⇒ Object



81
82
83
# File 'lib/altcha-rails.rb', line 81

def to_json(*args)
  to_h.to_json(*args)
end