Method: Password.urandom

Defined in:
lib/password.rb

.urandom(length = 8) ⇒ Object

An alternative to Password.random. It uses the /dev/urandom device to generate passwords, returning nil on systems that do not implement the device. The passwords it generates may contain any of the characters in Password::PASSWD_CHARS, plus the additional characters + and /.



386
387
388
389
390
391
392
393
394
# File 'lib/password.rb', line 386

def Password.urandom(length=8)
  return nil unless File.chardev? '/dev/urandom'

  rand_data = nil
  File.open( "/dev/urandom" ) { |f| rand_data = f.read( length ) }

  # Base64 encode it
  Password.new( [ rand_data ].pack( 'm' )[ 0 .. length - 1 ] )
end