Method: Password#crypt

Defined in:
lib/password.rb

#crypt(type = DES, salt = '') ⇒ Object

Encrypt a password using type encryption. salt, if supplied, will be used to perturb the encryption algorithm and should be chosen from the Password::SALT_CHARS. If no salt is given, a randomly generated salt will be used.



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/password.rb', line 402

def crypt(type=DES, salt='')

  unless ( salt.split( // ) - SALT_CHARS.split( // ) ).empty?
    raise CryptError, 'bad salt'
  end

  salt = Password.random( type ? 2 : 8 ) if salt.empty?

  # (Linux glibc2 interprets a salt prefix of '$1$' as a call to use MD5
  # instead of DES when calling crypt(3))
  salt = '$1$' + salt if type == MD5

  # Pass to crypt in class String (our parent class)
  crypt = super( salt )

  # Raise an exception if MD5 was wanted, but result is not recognisable
  if type == MD5 && crypt !~ /^\$1\$/
    raise CryptError, 'MD5 not implemented'
  end

  crypt
end