Class: ActiveModel::SecurePassword::Argon2Password

Inherits:
Object
  • Object
show all
Defined in:
activemodel/lib/active_model/secure_password/argon2_password.rb

Instance Method Summary collapse

Constructor Details

#initializeArgon2Password

Returns a new instance of Argon2Password.



6
7
8
9
10
11
12
13
14
# File 'activemodel/lib/active_model/secure_password/argon2_password.rb', line 6

def initialize
  # Load argon2 gem only when has_secure_password with :argon2 is used.
  # This is to avoid Active Model (and by extension the entire framework)
  # being dependent on a binary library.
  require "argon2"
rescue LoadError
  warn "You don't have argon2 installed in your application. Please add it to your Gemfile and run bundle install."
  raise
end

Instance Method Details

#algorithm_nameObject

Returns the algorithm name.



41
42
43
# File 'activemodel/lib/active_model/secure_password/argon2_password.rb', line 41

def algorithm_name
  :argon2
end

#hash_password(unencrypted_password) ⇒ Object

Hashes the unencrypted password using Argon2.



17
18
19
20
21
22
23
# File 'activemodel/lib/active_model/secure_password/argon2_password.rb', line 17

def hash_password(unencrypted_password)
  if ActiveModel::SecurePassword.min_cost
    ::Argon2::Password.new(profile: :unsafe_cheapest).create(unencrypted_password)
  else
    ::Argon2::Password.create(unencrypted_password)
  end
end

#password_salt(digest) ⇒ Object

Generates the salt from the password digest.



31
32
33
# File 'activemodel/lib/active_model/secure_password/argon2_password.rb', line 31

def password_salt(digest)
  ::Argon2::HashFormat.new(digest).salt
end

#validate(_record, _attribute) ⇒ Object

Validates the password and adds error to the record in the given attribute. Argon2 has no maximum input size, no validation needed.



37
38
# File 'activemodel/lib/active_model/secure_password/argon2_password.rb', line 37

def validate(_record, _attribute)
end

#verify_password(password, digest) ⇒ Object

Verifies if the password matches the digest.



26
27
28
# File 'activemodel/lib/active_model/secure_password/argon2_password.rb', line 26

def verify_password(password, digest)
  ::Argon2::Password.verify_password(password, digest)
end