Class: SequelAuth::Providers::Crypt

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel_auth/providers/crypt.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.methodObject

Salt prefix - prefix for random salt



10
11
12
# File 'lib/sequel_auth/providers/crypt.rb', line 10

def method
  @method ||= defaults[:method]
end

.saltObject

Salt size



20
21
22
# File 'lib/sequel_auth/providers/crypt.rb', line 20

def salt
  @salt ||= defaults[:salt]
end

.salt_sizeObject

Salt size - size as number



15
16
17
# File 'lib/sequel_auth/providers/crypt.rb', line 15

def salt_size
  @salt_size ||= defaults[:salt_size]
end

Class Method Details

.defaultsObject



42
43
44
45
46
47
48
# File 'lib/sequel_auth/providers/crypt.rb', line 42

def defaults
  {
    salt: nil,
    method: :sha512,
    salt_size: 16,
  }.freeze
end

.encrypt(password) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/sequel_auth/providers/crypt.rb', line 30

def encrypt(password)
  if salt
    password.crypt(salt)
  else
    password.crypt(random_salt)
  end
end

.matches?(hash, password) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/sequel_auth/providers/crypt.rb', line 38

def matches?(hash, password)
  password.crypt(hash) == hash
end

.random_saltObject



24
25
26
27
28
# File 'lib/sequel_auth/providers/crypt.rb', line 24

def random_salt
  schemes.fetch(method) + (salt_size-schemes.fetch(method).length).times.map { 
    (('a'..'z').to_a + (1..9).to_a + ('A'..'Z').to_a).sample 
  }.join
end