Module: Authtools::Password

Extended by:
Common, Password
Included in:
Password
Defined in:
lib/authtools/password.rb

Instance Method Summary collapse

Methods included from Common

salt

Instance Method Details

#check(password, store) ⇒ Object

Checks the password against the stored password.

Examples

# In `store` is hash generated before (see `generate` method).
Authtools::Password.check('mysecret', store) # => true
Authtools::Password.check('fake', store)     # => false


37
38
39
40
41
42
43
44
45
# File 'lib/authtools/password.rb', line 37

def check(password, store)
  hash = get_hash(store)
  salt = get_salt(store)
  if self.hash(password, salt) == hash
    true
  else
    false
  end
end

#generate(password) ⇒ Object

Generates a new salt and rehashes the password. Returns mixed hash.

Examples

store = Authtools::Password.generate('mysecret') 
  # => "f7d8f299e342168b7a8b0aeece32e090c4acced13a6bd7f2b26fc
  # 88251f550943820d190df00a87d20b7bc00cee332c48f9c4953793837
  # 2a6c4fbcbe5d3944ccr1x6DlrfTf6OUrwl6ohoivxN2fAQiblav1sLyd9
  # z7PFaQgQH3XxTA0BuMAbFRmMM"


18
19
20
21
22
# File 'lib/authtools/password.rb', line 18

def generate(password)
  salt = self.salt
  hash = self.hash(password, salt)
  store(hash, salt)
end

#get_hash(store) ⇒ Object

Gets the hash from a stored password.



61
62
63
# File 'lib/authtools/password.rb', line 61

def get_hash(store)
  store[0..127]
end

#get_salt(store) ⇒ Object

Gets the salt from a stored password.



67
68
69
# File 'lib/authtools/password.rb', line 67

def get_salt(store)
  store[128..192]
end

#hash(password, salt) ⇒ Object

Generates a 128 character hash.



49
50
51
# File 'lib/authtools/password.rb', line 49

def hash(password, salt)
  Digest::SHA512.hexdigest("#{password}:#{salt}")
end

#new(password) ⇒ Object

Alias for generate method



26
27
28
# File 'lib/authtools/password.rb', line 26

def new(password)
  generate(password)
end

#store(hash, salt) ⇒ Object

Mixes the hash and salt together for storage.



55
56
57
# File 'lib/authtools/password.rb', line 55

def store(hash, salt)
  hash + salt
end