Sha256: fd8e957945b294fba7c00fd2a273c67337908bb1d8e4ae18b4670f2faf660701

Contents?: true

Size: 1.92 KB

Versions: 8

Compression:

Stored size: 1.92 KB

Contents

# frozen-string-literal: true

require 'argon2'

# :nocov:
if !defined?(Argon2::VERSION) || Argon2::VERSION < '2'
  raise LoadError, "argon2 version 1.x not supported as it does not support argon2id hashes"
end
# :nocov:

module Rodauth
  Feature.define(:argon2, :Argon2) do
    depends :login_password_requirements_base

    auth_value_method :use_argon2?, true

    private

    if Argon2::VERSION != '2.1.0'
      def argon2_salt_option
        :salt_do_not_supply
      end
    # :nocov:
    else
      def argon2_salt_option
        :salt_for_testing_purposes_only
      end
    # :nocov:
    end

    def password_hash_cost
      return super unless use_argon2?
      argon2_hash_cost 
    end

    def password_hash(password)
      return super unless use_argon2?
      ::Argon2::Password.new(password_hash_cost).create(password)
    end

    def password_hash_match?(hash, password)
      return super unless argon2_hash_algorithm?(hash)
      argon2_password_hash_match?(hash, password)
    end

    def password_hash_using_salt(password, salt)
      return super unless argon2_hash_algorithm?(salt)

      argon2_params = Hash[extract_password_hash_cost(salt)]
      argon2_params[argon2_salt_option] = Base64.decode64(salt.split('$').last)
      ::Argon2::Password.new(argon2_params).create(password)
    end

    def extract_password_hash_cost(hash)
      return super unless argon2_hash_algorithm?(hash )

      /\A\$argon2id\$v=\d+\$m=(\d+),t=(\d+)/ =~ hash
      { t_cost: $2.to_i, m_cost: Math.log2($1.to_i).to_i }
    end

    if ENV['RACK_ENV'] == 'test'
      def argon2_hash_cost
        {t_cost: 1, m_cost: 3}
      end
    # :nocov:
    else
      def argon2_hash_cost
        {t_cost: 2, m_cost: 16}
      end
    end
    # :nocov:

    def argon2_hash_algorithm?(hash)
      hash.start_with?('$argon2id$')
    end

    def argon2_password_hash_match?(hash, password)
      ::Argon2::Password.verify_password(password, hash)
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rodauth-2.25.0 lib/rodauth/features/argon2.rb
rodauth-2.24.0 lib/rodauth/features/argon2.rb
rodauth-2.23.0 lib/rodauth/features/argon2.rb
rodauth-2.22.0 lib/rodauth/features/argon2.rb
rodauth-2.21.0 lib/rodauth/features/argon2.rb
rodauth-2.20.0 lib/rodauth/features/argon2.rb
rodauth-2.19.0 lib/rodauth/features/argon2.rb
rodauth-2.18.0 lib/rodauth/features/argon2.rb