memorable_password 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -22,6 +22,11 @@ Generates a password with a specified length.
22
22
  MemorablePassword.generate :length => 10
23
23
  => "june3eaten"
24
24
 
25
+ Generates a password that is at least a certain length.
26
+
27
+ MemorablePassword.generate :min_length => 8
28
+ => "gale3covalt"
29
+
25
30
  Generates a password that includes special characters.
26
31
 
27
32
  MemorablePassword.generate :special_characters => true
@@ -1,3 +1,3 @@
1
1
  module MemorablePassword
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -9,7 +9,8 @@ module MemorablePassword
9
9
  DEFAULT_OPTIONS = {
10
10
  :mixed_case => false,
11
11
  :special_characters => false,
12
- :length => nil
12
+ :length => nil,
13
+ :min_length => nil
13
14
  }
14
15
 
15
16
  class << self; attr_accessor :dictionary, :blacklist end
@@ -19,13 +20,20 @@ module MemorablePassword
19
20
  def self.generate(opts={})
20
21
  opts = DEFAULT_OPTIONS.merge(opts)
21
22
 
23
+ raise "You cannot specify :length and :min_length at the same time" if opts[:length] && opts[:min_length] # Nonsense!
24
+
22
25
  if opts[:length]
23
26
  password = [(opts[:length] >= 8 ? long_word : word), (opts[:special_characters] ? character : digit)]
24
27
  password << word(opts[:length] - password.compact.join.length)
25
28
 
26
29
  if (count = opts[:length] - password.compact.join.length) > 0
27
- count.times{ password << digit }
30
+ if count == 1
31
+ password << digit
32
+ else
33
+ password << word(count)
34
+ end
28
35
  end
36
+
29
37
  else
30
38
  if opts[:special_characters]
31
39
  password = [word, character, word, digit]
@@ -38,9 +46,26 @@ module MemorablePassword
38
46
  password.compact.reject{|x| x.length == 1}.sample.capitalize!
39
47
  end
40
48
 
49
+ # If a minimum length is required and this password is too short
50
+ if opts[:min_length] && password.compact.join.length < opts[:min_length]
51
+ if (count = opts[:min_length] - password.compact.join.length) == 1
52
+ password << digit
53
+ else
54
+ password << word(count)
55
+ end
56
+ end
57
+
41
58
  # If it is too long, just cut it down to size. This should not happen often unless the :length option is present and is very small.
42
59
  if opts[:length] && password.compact.join.length > opts[:length]
43
- password.compact.join.slice(0, opts[:length])
60
+ result = password.compact.join.slice(0, opts[:length])
61
+
62
+ # If there is no digit then it is probably a short password that by chance is just a dictionary word. Override that because that is bad.
63
+ if result =~ /^[a-z]+$/
64
+ password = [(opts[:mixed_case] ? word(opts[:length] - 1).capitalize : word(opts[:length] - 1)), (opts[:special_characters] ? character : digit)]
65
+ result = password.compact.join
66
+ end
67
+
68
+ result
44
69
  else
45
70
  password.compact.join
46
71
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: memorable_password
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kevin McPhillips
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-16 00:00:00 -05:00
18
+ date: 2011-06-05 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies: []
21
21