Class: UnixCrypt::Base

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

Direct Known Subclasses

DES, MD5, SHABase

Class Method Summary collapse

Class Method Details

.build(password, salt = nil, rounds = nil) ⇒ Object



2
3
4
5
6
7
8
9
# File 'lib/unix_crypt/base.rb', line 2

def self.build(password, salt = nil, rounds = nil)
  salt ||= generate_salt
  if salt.length > max_salt_length
    raise UnixCrypt::SaltTooLongError, "Salts longer than #{max_salt_length} characters are not permitted"
  end

  construct_password(password, salt, rounds)
end

.generate_saltObject



15
16
17
18
19
# File 'lib/unix_crypt/base.rb', line 15

def self.generate_salt
  # Generates a random salt using the same character set as the base64 encoding
  # used by the hash encoder.
  SecureRandom.base64((default_salt_length * 6 / 8.0).ceil).tr("+", ".")[0...default_salt_length]
end

.hash(password, salt, rounds = nil) ⇒ Object



11
12
13
# File 'lib/unix_crypt/base.rb', line 11

def self.hash(password, salt, rounds = nil)
  bit_specified_base64encode internal_hash(prepare_password(password), salt, rounds)
end