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
-
.delimiters=(string) ⇒ Object
Sets the list of characters that can delimit words.
-
.diction=(path) ⇒ Object
Sets the dictionary.
-
.generate(length = DEFAULT, options = {}) ⇒ Object
Generates a more memorable password.
- .generate_fun ⇒ Object
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.(__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"
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, = {}) unless defined? @@diction or [:animal] self.diction = PATHS.find { |path| File.exist? path } end = { :use_delimiter=>true, :use_number=>true, :animal => false, :variable_length=>false }.merge() if length && length.to_s.upcase == "ANY" length = rand(DEFAULT)+DEFAULT [: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 [:animal] @@animals = IO.readlines ANIMALS @@adjectives = IO.readlines ADJECTIVES end words_limit = length * 0.75 # Ensure over-proportionate word lengths. begin if [:animal] words = %W(#{random_adj} #{random_animal}) else words = %W(#{random_word} #{random_delimiter if [:use_delimiter]}#{random_word}) end words_length = words.join.length return words.join if (words_length == length && ![:use_number]) || [:variable_length] end until words_length < length && words_length > words_limit && [:use_number] words.join random_number(length - words_length) if [:use_number] end |
.generate_fun ⇒ Object
73 74 75 |
# File 'lib/haddock.rb', line 73 def generate_fun generate("any", {:use_delimiter=>false, :use_number=>false, :animal=>true}) end |