|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "argon2" |
| 4 | + |
| 5 | +module Authlogic |
| 6 | + module CryptoProviders |
| 7 | + # Argon2id is the recommended variant of the Argon2 password hashing |
| 8 | + # algorithm, which won the Password Hashing Competition in 2015. It |
| 9 | + # combines the side-channel resistance of Argon2i with the GPU/ASIC |
| 10 | + # attack resistance of Argon2d, making it the best choice for |
| 11 | + # password hashing. |
| 12 | + # |
| 13 | + # Argon2id has three configurable cost parameters: |
| 14 | + # |
| 15 | + # - t_cost: Number of iterations (time cost). Higher values increase |
| 16 | + # computation time. Default: 2 |
| 17 | + # - m_cost: Memory usage in powers of 2 (in kibibytes). |
| 18 | + # For example, m_cost of 16 means 2^16 KiB = 64 MiB. |
| 19 | + # Default: 16 (64 MiB) |
| 20 | + # - p_cost: Degree of parallelism (number of threads). Default: 1 |
| 21 | + # |
| 22 | + # To use Argon2id, install the argon2 gem: |
| 23 | + # |
| 24 | + # gem install argon2 |
| 25 | + # |
| 26 | + # Tell acts_as_authentic to use it: |
| 27 | + # |
| 28 | + # acts_as_authentic do |c| |
| 29 | + # c.crypto_provider = Authlogic::CryptoProviders::Argon2id |
| 30 | + # end |
| 31 | + # |
| 32 | + # To transition from another provider (lazy migration on login): |
| 33 | + # |
| 34 | + # acts_as_authentic do |c| |
| 35 | + # c.crypto_provider = Authlogic::CryptoProviders::Argon2id |
| 36 | + # c.transition_from_crypto_providers = [Authlogic::CryptoProviders::SCrypt] |
| 37 | + # end |
| 38 | + # |
| 39 | + # To update cost parameters (existing passwords are re-hashed on |
| 40 | + # next login): |
| 41 | + # |
| 42 | + # Authlogic::CryptoProviders::Argon2id.t_cost = 3 |
| 43 | + # Authlogic::CryptoProviders::Argon2id.m_cost = 17 |
| 44 | + # |
| 45 | + class Argon2id |
| 46 | + class << self |
| 47 | + attr_writer :t_cost, :m_cost, :p_cost |
| 48 | + |
| 49 | + # Time cost (number of iterations). Default: 2 |
| 50 | + def t_cost |
| 51 | + @t_cost ||= 2 |
| 52 | + end |
| 53 | + |
| 54 | + # Memory cost as a power of 2 (in kibibytes). Default: 16 (64 MiB) |
| 55 | + def m_cost |
| 56 | + @m_cost ||= 16 |
| 57 | + end |
| 58 | + |
| 59 | + # Parallelism (number of threads). Default: 1 |
| 60 | + def p_cost |
| 61 | + @p_cost ||= 1 |
| 62 | + end |
| 63 | + |
| 64 | + # Creates an Argon2id hash for the password passed. |
| 65 | + def encrypt(*tokens) |
| 66 | + hasher = ::Argon2::Password.new( |
| 67 | + t_cost: t_cost, |
| 68 | + m_cost: m_cost, |
| 69 | + p_cost: p_cost |
| 70 | + ) |
| 71 | + hasher.create(join_tokens(tokens)) |
| 72 | + end |
| 73 | + |
| 74 | + # Does the hash match the tokens? Uses the same tokens that were |
| 75 | + # used to encrypt. |
| 76 | + def matches?(hash, *tokens) |
| 77 | + return false if hash.blank? |
| 78 | + ::Argon2::Password.verify_password(join_tokens(tokens), hash) |
| 79 | + rescue ::Argon2::ArgonHashFail |
| 80 | + false |
| 81 | + end |
| 82 | + |
| 83 | + # Checks whether the existing hash uses the same cost parameters |
| 84 | + # as the current configuration. If not, Authlogic will re-hash |
| 85 | + # the password on next successful login. |
| 86 | + def cost_matches?(hash) |
| 87 | + return false if hash.blank? |
| 88 | + params = extract_params(hash) |
| 89 | + return false if params.nil? |
| 90 | + params[:t] == t_cost && |
| 91 | + params[:m] == (1 << m_cost) && |
| 92 | + params[:p] == p_cost |
| 93 | + end |
| 94 | + |
| 95 | + private |
| 96 | + |
| 97 | + def join_tokens(tokens) |
| 98 | + tokens.flatten.join |
| 99 | + end |
| 100 | + |
| 101 | + # Parses cost parameters from an Argon2id hash string. |
| 102 | + # Format: $argon2id$v=19$m=65536,t=2,p=1$salt$hash |
| 103 | + def extract_params(hash) |
| 104 | + match = hash.to_s.match(/\$argon2id?\$v=\d+\$m=(\d+),t=(\d+),p=(\d+)\$/) |
| 105 | + return nil unless match |
| 106 | + { m: match[1].to_i, t: match[2].to_i, p: match[3].to_i } |
| 107 | + end |
| 108 | + end |
| 109 | + end |
| 110 | + end |
| 111 | +end |
0 commit comments