Module: Haddock::Password

Defined in:
lib/haddock.rb

Defined Under Namespace

Classes: LengthError, NoWordsError

Constant Summary collapse

MINIMUM =

The minimum password legnth.

8
MAXIMUM =

The maximum password length.

31
DEFAULT =

The default password length.

12
PATHS =

Paths used to detect default words files.

%w(/usr/share/dict/words /usr/share/words)
ADJECTIVES =
File.dirname(__FILE__) +"/adjectives.txt"
ANIMALS =
File.dirname(__FILE__) +"/animals.txt"
@@delimiters =
'`~!@#$%^&*()-_=+[{]}\\|;:\'",<.>/?'

Class Method Summary collapse

Class Method Details

.delimiters=(string) ⇒ Object

Sets the list of characters that can delimit words. Default:

`~!@#$%^&*()-_=+[{]}\|;:'",<.>/?


92
93
94
# File 'lib/haddock.rb', line 92

def delimiters=(string)
  @@delimiters = string
end

.diction=(path) ⇒ Object

Sets the dictionary. Uses “/usr/share/dict/words” or “/usr/share/words” otherwise.

Password.diction = File.expand_path(__FILE__) + "/my_words.txt"


81
82
83
84
85
86
87
# File 'lib/haddock.rb', line 81

def diction=(path)
  @@diction = IO.readlines path
rescue TypeError
  raise NoWordsError, "No words file found"
rescue Errno::ENOENT
  raise NoWordsError, "No words file at #{path.inspect}"
end

.generate(length = DEFAULT, options = {}) ⇒ Object

Generates a more memorable password. Its one optional argument determines the length of the generated password, and cannot be less than 8 or greater than 31 characters (default: 12). Allows simplification with options

Password.generate     # => "bowl9&bracky"
Password.generate(30) # => "Phlebotomus2473?nonconditioned"
Password.generate(8)  # => "amy7@rax"
Password.generate(8, {:use_delimeter=>false} => "bonk5and"
Password.generate(8, {:use_numbers=>false} => "jack!man"

Raises:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/haddock.rb', line 37

def generate(length = DEFAULT, options = {})
  unless defined? @@diction or options[:animal]
	  self.diction = PATHS.find { |path| File.exist? path }
  end
	options = {
	  :use_delimiter=>true, 
	  :use_number=>true,
	  :animal => false,
	  :variable_length=>false
  }.merge(options)
	if length && length.to_s.upcase == "ANY"
	  length = rand(DEFAULT)+DEFAULT 
	  options[:variable_length] = true
	end
  raise LengthError, "Invalid length" unless length.is_a? Integer
  raise LengthError, "Password length is too short" if length < MINIMUM
  raise LengthError, "Password length is too long" if length > MAXIMUM
	if options[:animal]
	  @@animals = IO.readlines ANIMALS 
	  @@adjectives = IO.readlines ADJECTIVES
	end
  words_limit = length * 0.75 # Ensure over-proportionate word lengths.

  begin
	  if options[:animal]
      words = %W(#{random_adj} #{random_animal})
	  else
      words = %W(#{random_word} #{random_delimiter if options[:use_delimiter]}#{random_word})
    end
	  words_length = words.join.length
	  return words.join if (words_length == length && !options[:use_number]) || options[:variable_length]
  end until words_length < length && words_length > words_limit && options[:use_number]
  
	words.join random_number(length - words_length) if options[:use_number]
end

.generate_funObject



73
74
75
# File 'lib/haddock.rb', line 73

def generate_fun
	generate("any", {:use_delimiter=>false, :use_number=>false, :animal=>true})
end