Module: Sinatra::Security::Password::Hashing

Extended by:
Hashing
Included in:
Hashing
Defined in:
lib/sinatra/security/password.rb

Instance Method Summary collapse

Instance Method Details

#check(password, crypted) ⇒ true, false

Checks the password against the serialized password.

Examples:


include Sinatra::Security
crypted = Password::Hashing.encrypt('123')
Password::Hashing.check('123', crypted)
# => true

Parameters:

  • password (String)

    a string to check against crypted.

  • crypted (String)

    the serialized string containing the hash and salt.

Returns:

  • (true)

    if the password matches the hash / salt combination.

  • (false)

    if the password does not match the hash / salt.



89
90
91
92
93
# File 'lib/sinatra/security/password.rb', line 89

def check(password, crypted)
  hash, salt = unserialize(crypted)

  self.hash(password, salt) == hash
end

#encrypt(password, salt = self.generate_salt) ⇒ String

Given any string generates a string which includes both the 128 character (SHA512) hash and the salt.

By default the salt is a 64 character pseudo-random string.

Examples:


include Sinatra::Security

crypted = Password::Hashing.encrypt('123')
crypted.length == 192
# => true

crypted = Password::Hashing.encrypt('123', '123456789012')
crypted.length == 140 # i.e. 128 + 12 chars
# => true

crypted = Password::Hashing.encrypt('123')
Password::Hashing.check('123', crypted)
# => true

Parameters:

  • password (String)

    any string with any length.

  • salt (String) (defaults to: self.generate_salt)

    (defaults to Hashing#salt) any string.

Returns:

  • (String)

    a string holding the crypted password and the salt.

See Also:



70
71
72
# File 'lib/sinatra/security/password.rb', line 70

def encrypt(password, salt = self.generate_salt)
  serialize(hash(password, salt), salt)
end