cryptice-passgen 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,6 +12,7 @@ non-Rails applications as well.
12
12
 
13
13
  The usage could not be easier. Just require and call the generate method:
14
14
 
15
+ >> require 'rubygems'
15
16
  >> require 'passgen'
16
17
  >> Passgen::generate
17
18
  => "zLWCeS3xC9"
data/Rakefile CHANGED
@@ -3,7 +3,7 @@ require 'rubygems'
3
3
  require 'rake'
4
4
  require 'echoe'
5
5
 
6
- Echoe.new('passgen', '0.1.1') do |p|
6
+ Echoe.new('passgen', '0.1.2') do |p|
7
7
  p.description = "A password generation gem for Ruby and Rails applications."
8
8
  p.url = "http://github.com/cryptice/passgen"
9
9
  p.author = "Erik Lindblad"
@@ -1,3 +1,126 @@
1
+ #= Passgen
2
+ #
3
+ #Ruby gem for generating passwords quickly and easily. Although it is
4
+ #suitable for use within Rails it has no Rails dependencies and can be used in
5
+ #non-Rails applications as well.
6
+ #
7
+ #== Install
8
+ #
9
+ # gem install cryptice-passgen --source http://gems.github.com
10
+ #
11
+ #== Usage
12
+ #
13
+ #The usage could not be easier. Just require and call the generate method:
14
+ #
15
+ # >> require 'rubygems'
16
+ # >> require 'passgen'
17
+ # >> Passgen::generate
18
+ # => "zLWCeS3xC9"
19
+ #
20
+ #== Examples
21
+ #
22
+ # >> Passgen::generate
23
+ # => "zLWCeS3xC9"
24
+ #
25
+ # >> Passgen::generate(:length => 20)
26
+ # => "6lCcHvkuEW6OuzAtkoAs"
27
+ #
28
+ # >> Passgen::generate(:symbols => true)
29
+ # => "gr)$6bIym1"
30
+ #
31
+ # >> Passgen::generate(:lowercase => :only)
32
+ # => "ysbwuxbcea"
33
+ #
34
+ # >> Passgen::generate(:number => 3)
35
+ # => ["REdOigTkdI", "PQu8DsV9WZ", "qptKLbw8YQ"]
36
+ #
37
+ # >> Passgen::generate(:seed => 5)
38
+ # => "JoV9M2qjiK"
39
+ # >> Passgen::generate(:seed => 5) # Will generate same password again
40
+ # => "JoV9M2qjiK"
41
+ #
42
+ # >> Passgen::generate(:seed => :default) # Will set random seed...
43
+ # => "SI8QDBdV98"
44
+ # >> Passgen::generate(:seed => :default) # and hence give different password
45
+ # => "tHHU5HLBAn"
46
+ #
47
+ #== Options:
48
+ #
49
+ #=== :lowercase => true/false/:only
50
+ #* true - Use lowercase letters in the generated password.
51
+ #* false - Do not use lowercase letters in the generated password.
52
+ #* :only - Only use lowercase letters in the generated password.
53
+ #
54
+ #=== :uppercase => true/false/:only
55
+ #* true - Use uppercase letters in the generated password.
56
+ #* false - Do not use uppercase letters in the generated password.
57
+ #* :only - Only use uppercase letters in the generated password.
58
+ #
59
+ #=== :digits => true/false/:only
60
+ #* true - Use digits in the generated password.
61
+ #* false - Do not use digits in the generated password.
62
+ #* :only - Only use digits in the generated password.
63
+ #
64
+ #=== :symbols => true/false/:only/:list
65
+ #* true - Use symbols in the generated password.
66
+ #* false - Do not use symbols in the generated password.
67
+ #* :only - Only use symbols in the generated password.
68
+ #* :list - A string with the symbols to use. Not implemented yet.
69
+ #
70
+ #=== :pronounceable => true/false
71
+ #Not implmented yet.
72
+ #
73
+ #=== :number => integer
74
+ #Number of passwords to generate. If >1 the result is an Array.
75
+ #
76
+ #=== :length => integer/range
77
+ #The number of characters in the generated passwords. A range results in passwords
78
+ #lengths within the given range.
79
+ #
80
+ #=== :seed => integer/:default
81
+ #Set the srand seed to the given integer prior to generating the passwords.
82
+ #
83
+ #=== Default values:
84
+ #
85
+ #:lowercase => true
86
+ #
87
+ #:uppercase => true
88
+ #
89
+ #:digits => true
90
+ #
91
+ #:symbols => false
92
+ #
93
+ #:pronounceable => Not implemented yet.
94
+ #
95
+ #:number => 1
96
+ #
97
+ #:length => 10
98
+ #
99
+ #:seed => nil
100
+ #
101
+ #== Copyright and license
102
+ #
103
+ #Copyright (c) 2009 Erik Lindblad
104
+ #
105
+ #Permission is hereby granted, free of charge, to any person obtaining
106
+ #a copy of this software and associated documentation files (the
107
+ #"Software"), to deal in the Software without restriction, including
108
+ #without limitation the rights to use, copy, modify, merge, publish,
109
+ #distribute, sublicense, and/or sell copies of the Software, and to
110
+ #permit persons to whom the Software is furnished to do so, subject to
111
+ #the following conditions:
112
+ #
113
+ #The above copyright notice and this permission notice shall be
114
+ #included in all copies or substantial portions of the Software.
115
+ #
116
+ #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
117
+ #EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
118
+ #MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
119
+ #NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
120
+ #LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
121
+ #OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
122
+ #WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
123
+
1
124
  require "digest"
2
125
 
3
126
  module Passgen
@@ -11,6 +134,10 @@ module Passgen
11
134
  :symbols => false
12
135
  }
13
136
 
137
+ def self.default_seed
138
+ Digest::MD5.hexdigest("#{rand}#{Time.now}#{Process.object_id}").to_i(16)
139
+ end
140
+
14
141
  def self.generate(params={})
15
142
  set_options(params)
16
143
  tokens = valid_tokens
@@ -23,20 +150,11 @@ module Passgen
23
150
  end
24
151
  end
25
152
 
153
+ private
26
154
  def self.generate_one(tokens)
27
155
  Array.new(password_length) {tokens[rand(tokens.size)]}.join
28
156
  end
29
157
 
30
- def self.set_seed
31
- if @options[:seed]
32
- if @options[:seed] == :default
33
- srand(Digest::MD5.hexdigest("#{rand}#{Time.now}#{Process.object_id}").to_i(16))
34
- else
35
- srand(@options[:seed])
36
- end
37
- end
38
- end
39
-
40
158
  def self.password_length
41
159
  if @options[:length].is_a?(Range)
42
160
  tmp = @options[:length].to_a
@@ -76,12 +194,54 @@ module Passgen
76
194
  @options = DEFAULT_PARAMS.merge(params)
77
195
  end
78
196
 
197
+ def self.set_seed
198
+ if @options[:seed]
199
+ if @options[:seed] == :default
200
+ srand(default_seed)
201
+ else
202
+ srand(@options[:seed])
203
+ end
204
+ end
205
+ end
206
+
207
+ def self.lowercase_tokens
208
+ ("a".."z").to_a
209
+ end
210
+
211
+ def self.uppercase_tokens
212
+ ("A".."Z").to_a
213
+ end
214
+
215
+ def self.digit_tokens
216
+ ("0".."9").to_a
217
+ end
218
+
219
+ def self.symbol_tokens
220
+ %w{! @ # $ % & / ( ) + ? *}
221
+ end
222
+
223
+ def self.use_lowercase?
224
+ @options[:lowercase]
225
+ end
226
+
227
+ def self.use_uppercase?
228
+ @options[:uppercase]
229
+ end
230
+
231
+ def self.use_digits?
232
+ @options[:digits]
233
+ end
234
+
235
+ def self.use_symbols?
236
+ @options[:symbols]
237
+ end
238
+
79
239
  def self.valid_tokens
80
240
  tmp = []
81
- tmp += ("a".."z").to_a if @options[:lowercase]
82
- tmp += ("A".."Z").to_a if @options[:uppercase]
83
- tmp += ("0".."9").to_a if @options[:digits]
84
- tmp += %w{! @ # $ % & / ( ) + ? *} if @options[:symbols]
241
+ tmp += lowercase_tokens if use_lowercase?
242
+ tmp += uppercase_tokens if use_uppercase?
243
+ tmp += digit_tokens if use_digits?
244
+ tmp += symbol_tokens if use_symbols?
85
245
  tmp
86
246
  end
87
247
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{passgen}
5
- s.version = "0.1.1"
5
+ s.version = "0.1.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Erik Lindblad"]
@@ -92,5 +92,24 @@ describe "Using passgen" do
92
92
  pass2 = Passgen::generate(:seed => :default)
93
93
  pass1.should_not eql(pass2)
94
94
  end
95
-
96
- end
95
+
96
+ describe "handling tokens" do
97
+
98
+ it "should return a-z" do
99
+ Passgen::lowercase_tokens.should eql(("a".."z").to_a)
100
+ end
101
+
102
+ it "should return A-Z" do
103
+ Passgen::uppercase_tokens.should eql(("A".."Z").to_a)
104
+ end
105
+
106
+ it "should return 0-9" do
107
+ Passgen::digit_tokens.should eql(("0".."9").to_a)
108
+ end
109
+
110
+ it "should return default symbols" do
111
+ Passgen::symbol_tokens.should eql(%w{! @ # $ % & / ( ) + ? *})
112
+ end
113
+
114
+ end
115
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cryptice-passgen
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erik Lindblad