Class: PasswordHasher

Inherits:
Object
  • Object
show all
Defined in:
lib/password_hasher.rb

Defined Under Namespace

Classes: InvalidAlgorithmError, UnsupportedAlgorithmError

Constant Summary collapse

HANDLERS =
{}

Class Method Summary collapse

Class Method Details

.hash_password(password:, salt:, algorithm:) ⇒ Object

Algorithm should be specified according to the id/params parts of the PHC string format. github.com/P-H-C/phc-string-format/blob/master/phc-sf-spec.md



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/password_hasher.rb', line 19

def self.hash_password(password:, salt:, algorithm:)
  algorithm = algorithm.delete_prefix("$").delete_suffix("$")

  parts = algorithm.split("$")
  raise InvalidAlgorithmError if parts.length != 2

  algorithm_id, algorithm_params = parts

  algorithm_params = algorithm_params.split(",").map { |pair| pair.split("=") }.to_h

  handler = HANDLERS[algorithm_id]
  if handler.nil?
    raise UnsupportedAlgorithmError.new "#{algorithm_id} is not a supported password algorithm"
  end

  handler.call(password: password, salt: salt, params: algorithm_params)
end

.register_handler(id, &blk) ⇒ Object



12
13
14
# File 'lib/password_hasher.rb', line 12

def self.register_handler(id, &blk)
  HANDLERS[id] = blk
end