custom_password_generator 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/password_generator.rb +150 -0
  3. metadata +73 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c9d02bb8cf66fa3d786904e09693745fe737f506
4
+ data.tar.gz: cdd23226fea44a5d9a70724679dba42025c84a96
5
+ SHA512:
6
+ metadata.gz: 3dcb8acd810902ecd2365c3ad25ebd932f0fc5c95e59e6e23f3869e7243027014e869c097325d6917dbcaa72889375072b8e9e7f02cec485bb302007c323efe4
7
+ data.tar.gz: da66d9a46380639328696af9316517483a18316e3a513ed4bd3f63c4474385d5554a37caa4f9d2e969604a43871f96c035ab4b5791646a7fa2b4831530c49877
@@ -0,0 +1,150 @@
1
+ class PasswordGenerator
2
+ NUMS = ('0'..'9').to_a
3
+ LOWER_CASES = ('a'..'z').to_a
4
+ UPPER_CASES = ('A'..'Z').to_a
5
+ SPECIAL_CHARS = ['?', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '~', '.', ',', '<', '>', '[', ']']
6
+
7
+ MIN_LENGTH = 1
8
+ DEFAULT_LENGTH = 8
9
+
10
+ def self.generate(*opts)
11
+ opts = [{}] if opts.empty?
12
+
13
+ opts.inject("") do |password, generator_opts|
14
+ password << self.new(generator_opts).generate
15
+ end
16
+ end
17
+
18
+ def self.opts_for(type, length = DEFAULT_LENGTH)
19
+ case type
20
+ when :numeric
21
+ {
22
+ length: length,
23
+ include_nums: true,
24
+ include_lower_case: false,
25
+ include_upper_case: false,
26
+ include_special: false
27
+ }
28
+ when :lower
29
+ {
30
+ length: length,
31
+ include_nums: false,
32
+ include_lower_case: true,
33
+ include_upper_case: false,
34
+ include_special: false
35
+ }
36
+ when :upper
37
+ {
38
+ length: length,
39
+ include_nums: false,
40
+ include_lower_case: false,
41
+ include_upper_case: true,
42
+ include_special: false
43
+ }
44
+ when :special
45
+ {
46
+ length: length,
47
+ include_nums: false,
48
+ include_lower_case: false,
49
+ include_upper_case: false,
50
+ include_special: true
51
+ }
52
+ when :alpha
53
+ {
54
+ length: length,
55
+ include_nums: false,
56
+ include_lower_case: true,
57
+ include_upper_case: true,
58
+ include_special: false
59
+ }
60
+ when :alnum
61
+ {
62
+ length: length,
63
+ include_nums: true,
64
+ include_lower_case: true,
65
+ include_upper_case: true,
66
+ include_special: false
67
+ }
68
+ when :all
69
+ {
70
+ length: length,
71
+ include_nums: true,
72
+ include_lower_case: true,
73
+ include_upper_case: true,
74
+ include_special: true
75
+ }
76
+ else
77
+ {
78
+ length: length
79
+ }
80
+ end
81
+ end
82
+
83
+
84
+ def initialize(opts = {})
85
+ @length = opts[:length] || DEFAULT_LENGTH
86
+ raise "Length must be at least #{MIN_LENGTH}" if @length < MIN_LENGTH
87
+
88
+ @pools = []
89
+ @excluded = opts[:exclude] || []
90
+
91
+ if opts[:pool]
92
+ add_pool(opts[:pool])
93
+ include_other_pools_by_default = false
94
+ else
95
+ include_other_pools_by_default = true
96
+ end
97
+
98
+ @include_nums = opts[:include_nums]
99
+ @include_nums = include_other_pools_by_default if @include_nums.nil?
100
+
101
+ @include_lower_case = opts[:include_lower_case]
102
+ @include_lower_case = include_other_pools_by_default \
103
+ if @include_lower_case.nil?
104
+
105
+ @include_upper_case = opts[:include_upper_case]
106
+ @include_upper_case = include_other_pools_by_default \
107
+ if @include_upper_case.nil?
108
+
109
+ @include_special = opts[:include_special]
110
+ @include_special = include_other_pools_by_default if @include_special.nil?
111
+
112
+ add_pool(NUMS) if @include_nums
113
+ add_pool(LOWER_CASES) if @include_lower_case
114
+ add_pool(UPPER_CASES) if @include_upper_case
115
+ add_pool(SPECIAL_CHARS) if @include_special
116
+
117
+ if @pools.empty?
118
+ raise ArgumentError.new('Char pool is empty. Password cannot be generated')
119
+ end
120
+ end
121
+
122
+ def generate
123
+ password = ''
124
+
125
+ remaining_chars = @length
126
+
127
+ if remaining_chars >= @pools.length
128
+ @pools.each do |pool|
129
+ password << pool.sample
130
+ remaining_chars -= 1
131
+ end
132
+ end
133
+
134
+ remaining_chars.times do
135
+ password << @pools.sample.sample
136
+ end
137
+
138
+ password.split("").shuffle.join
139
+ end
140
+
141
+ private
142
+ def add_pool(pool)
143
+ sanitized_pool = sanitize_pool(pool)
144
+ @pools.push(sanitized_pool) unless sanitized_pool.empty?
145
+ end
146
+
147
+ def sanitize_pool(pool)
148
+ pool - @excluded
149
+ end
150
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: custom_password_generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jan Renra Gloser
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email:
43
+ - jan.renra.gloser@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/password_generator.rb
49
+ homepage: https://github.com/renra/password_generator_ruby
50
+ licenses:
51
+ - MIT
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 2.2.2
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Configurable strong-password generator
73
+ test_files: []