Class: Singularity::PasswordHash

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(digest = nil) ⇒ PasswordHash

Returns a new instance of PasswordHash.



8
9
10
# File 'lib/singularity/password_hash.rb', line 8

def initialize(digest = nil)
  @digest = digest
end

Instance Attribute Details

#digestObject (readonly)

Returns the value of attribute digest.



6
7
8
# File 'lib/singularity/password_hash.rb', line 6

def digest
  @digest
end

Class Method Details

.digest_algorithmObject



14
15
16
# File 'lib/singularity/password_hash.rb', line 14

def self.digest_algorithm
  OpenSSL::Digest::SHA512.new
end

.generate(password, salt, options = {}) ⇒ Object



22
23
24
25
# File 'lib/singularity/password_hash.rb', line 22

def self.generate(password, salt, options = {})
  iters = options.fetch(:iterations, iterations)
  OpenSSL::PKCS5.pbkdf2_hmac(password, salt, iters, key_length, digest_algorithm).unpack('H*').first
end

.iterationsObject



12
# File 'lib/singularity/password_hash.rb', line 12

def self.iterations; 20_000; end

.key_lengthObject



18
19
20
# File 'lib/singularity/password_hash.rb', line 18

def self.key_length
  digest_algorithm.length
end

Instance Method Details

#matches?(password, salt, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/singularity/password_hash.rb', line 27

def matches?(password, salt, options = {})
  Rack::Utils.secure_compare digest, self.class.generate(password, salt, options)
end