Module: UnixCrypt

Defined in:
lib/unix_crypt.rb

Defined Under Namespace

Classes: Base, MD5, SHA256, SHA512, SHABase

Constant Summary collapse

IDENTIFIER_MAPPINGS =
{'1' => MD5, '5' => SHA256, '6' => SHA512}

Class Method Summary collapse

Class Method Details

.valid?(password, string) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
7
8
9
10
11
# File 'lib/unix_crypt.rb', line 4

def self.valid?(password, string)
  # Handle the original DES-based crypt(3)
  return password.crypt(string) == string if string.length == 13

  return false unless m = string.match(/\A\$([156])\$(?:rounds=(\d+)\$)?(.+)\$(.+)/)
  hash = IDENTIFIER_MAPPINGS[m[1]].hash(password, m[3], m[2] && m[2].to_i)
  hash == m[4]
end