Module: UnixCrypt

Defined in:
lib/unix_crypt.rb,
lib/unix_crypt/sha.rb

Defined Under Namespace

Classes: Base, CommandLine, DES, MD5, SHA256, SHA512, SHABase

Constant Summary collapse

VERSION =
"1.3.1"
Error =
Class.new(StandardError)
SaltTooLongError =
Class.new(Error)
IDENTIFIER_MAPPINGS =
{
  '1' => UnixCrypt::MD5,
  '5' => UnixCrypt::SHA256,
  '6' => UnixCrypt::SHA512
}

Class Method Summary collapse

Class Method Details

.valid?(password, string) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
# File 'lib/unix_crypt.rb', line 10

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

  # All other types of password follow a standard format
  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