password_util 0.0.5 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4af24e1ec068268a89fcafaf6a05f968ac9f1128bbb8cbf805c13c76344bdd4a
4
- data.tar.gz: 8f8056f8479dde813d25628a799400c6a0b1cbc17ac436529b176159c6f7675b
3
+ metadata.gz: dfc4714b8ab73246626ae199a8d874ce89fbab223466529a3059a8a952b22a26
4
+ data.tar.gz: cb5de8dd65ecb31cd05fda34369cc97b979b94c66730743d49d4ee6b382ead8f
5
5
  SHA512:
6
- metadata.gz: 895f0bdd59d36c6b4b7aabc5790bda344bde777074ca12eeea46cf4e4d14ddb5c43ef4226b1e9c01047283dbd19a46fe420560cd017f777e056bfdd7d4210710
7
- data.tar.gz: 2af852bfde063c96556a7f818500b5ee6ca01ed972d91dfdcf8eed4b033dae5fb07b348d0138d58e133063d28d02d852ff7eadff170afcd80ccde41cb0b6fc7a
6
+ metadata.gz: 1858630fc1d0441bc54a4da9836165922480dcb776287c5602856b86692f30fd1163fa325a42b747e22421be47c02762ecf2f58026f83ff52fc744fcbb200e39
7
+ data.tar.gz: 7af824a87c53c09e64cc3e4125d2cd70fceb3c8a7bd49290eff0cc08b128ec8026f62a6f356ea67bc0017ff5bf82f75816b3e42682ee6dd19bc03d96c5a74fd7
@@ -0,0 +1,8 @@
1
+ module PasswordUtil
2
+ module CharacterSets
3
+ UPPER_LETTERS = ('A'..'Z').to_a.freeze
4
+ LOWER_LETTERS = ('a'..'z').to_a.freeze
5
+ NUMBERS = ('0'..'9').to_a.freeze
6
+ SYMBOLS = %w[~ ` ! @ # $ % ^ & * ( ) _ - + = { \[ } \] | \\ : ; " ' < , > . ? /].freeze
7
+ end
8
+ end
@@ -0,0 +1,56 @@
1
+ require_relative './character_sets'
2
+
3
+ module PasswordUtil
4
+ DEFAULT_PASSWORD_LENGTH = 8
5
+ DEFAULT_HAS_LOWER_LETTERS = true
6
+ DEFAULT_MIN_LOWER_LETTERS = 1
7
+ DEFAULT_HAS_UPPER_LETTERS = true
8
+ DEFAULT_MIN_UPPER_LETTERS = 1
9
+ DEFAULT_HAS_NUMBERS = true
10
+ DEFAULT_MIN_NUMBERS = 1
11
+ DEFAULT_HAS_SYMBOLS = true
12
+ DEFAULT_MIN_SYMBOLS = 1
13
+
14
+ class Config
15
+ class ConfigurationError < StandardError; end
16
+
17
+ attr_accessor :has_lower_letters, :has_numbers, :has_symbols, :has_upper_letters, :lower_letters, :min_lower_letters, :min_numbers, :min_symbols, :min_upper_letters, :numbers, :password_length, :symbols, :upper_letters
18
+
19
+ def initialize(params)
20
+ @upper_letters = params.fetch(:upper_letters, CharacterSets::UPPER_LETTERS)
21
+ @lower_letters = params.fetch(:lower_letters, CharacterSets::LOWER_LETTERS)
22
+ @numbers = params.fetch(:numbers, CharacterSets::NUMBERS)
23
+ @symbols = params.fetch(:symbols, CharacterSets::SYMBOLS)
24
+
25
+ @password_length = params.fetch(:password_length, DEFAULT_PASSWORD_LENGTH)
26
+
27
+ @has_lower_letters = params.fetch(:has_lower_letters, DEFAULT_HAS_LOWER_LETTERS)
28
+ @min_lower_letters = params.fetch(:min_lower_letters, DEFAULT_MIN_LOWER_LETTERS)
29
+
30
+ @has_upper_letters = params.fetch(:has_upper_letters, DEFAULT_HAS_UPPER_LETTERS)
31
+ @min_upper_letters = params.fetch(:min_upper_letters, DEFAULT_MIN_UPPER_LETTERS)
32
+
33
+ @has_numbers = params.fetch(:has_numbers, DEFAULT_HAS_NUMBERS)
34
+ @min_numbers = params.fetch(:min_numbers, DEFAULT_MIN_NUMBERS)
35
+
36
+ @has_symbols = params.fetch(:has_symbols, DEFAULT_HAS_SYMBOLS)
37
+ @min_symbols = params.fetch(:min_symbols, DEFAULT_MIN_SYMBOLS)
38
+ end
39
+
40
+ def validate!
41
+ boolean_classes = [TrueClass, FalseClass]
42
+
43
+ raise ConfigurationError, 'password_length: expected Integer' unless password_length.is_a?(Integer)
44
+ raise ConfigurationError, 'has_lower_letters: expected Boolean' unless boolean_classes.include?(has_lower_letters.class)
45
+ raise ConfigurationError, 'min_lower_letters: expected Integer' unless min_lower_letters.is_a?(Integer)
46
+ raise ConfigurationError, 'has_upper_letters: expected Boolean' unless boolean_classes.include?(has_upper_letters.class)
47
+ raise ConfigurationError, 'min_upper_letters: expected Integer' unless min_upper_letters.is_a?(Integer)
48
+ raise ConfigurationError, 'has_numbers: expected Boolean' unless boolean_classes.include?(has_numbers.class)
49
+ raise ConfigurationError, 'min_numbers: expected Integer' unless min_numbers.is_a?(Integer)
50
+ raise ConfigurationError, 'has_symbols: expected Boolean' unless boolean_classes.include?(has_symbols.class)
51
+ raise ConfigurationError, 'min_symbols: expected Integer' unless min_symbols.is_a?(Integer)
52
+
53
+ raise ConfigurationError, 'No usable character set.' unless has_lower_letters || has_upper_letters || has_numbers || has_symbols
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,50 @@
1
+ module PasswordUtil
2
+ class Generator
3
+ def initialize(config)
4
+ @config = config
5
+ end
6
+
7
+ def generate
8
+ config.validate!
9
+
10
+ charset = []
11
+ password = []
12
+
13
+ if config.has_upper_letters
14
+ charset << config.upper_letters
15
+ config.min_upper_letters.times do
16
+ password << config.upper_letters.sample
17
+ end
18
+ end
19
+
20
+ if config.has_lower_letters
21
+ charset << config.lower_letters
22
+ config.min_lower_letters.times do
23
+ password << config.lower_letters.sample
24
+ end
25
+ end
26
+
27
+ if config.has_numbers
28
+ charset << config.numbers
29
+ config.min_numbers.times do
30
+ password << config.numbers.sample
31
+ end
32
+ end
33
+
34
+ if config.has_symbols
35
+ charset << config.symbols
36
+ config.min_symbols.times do
37
+ password << config.symbols.sample
38
+ end
39
+ end
40
+
41
+ password.shuffle!
42
+ password << charset.sample.sample while password.length < config.password_length
43
+ password.join('')[0...config.password_length]
44
+ end
45
+
46
+ private
47
+
48
+ attr_reader :config
49
+ end
50
+ end
data/lib/password_util.rb CHANGED
@@ -1,113 +1,76 @@
1
- module PasswordUtil
2
- ##
3
- # Configuration exception.
4
- class ConfigurationError < StandardError; end
5
-
6
- UPPER_LETTERS = ('A'..'Z').to_a.freeze
7
- LOWER_LETTERS = ('a'..'z').to_a.freeze
8
- NUMBERS = ('0'..'9').to_a.freeze
9
- SYMBOLS = %w[~ ` ! @ # $ % ^ & * ( ) _ - + = { \[ } \] | \\ : ; " ' < , > . ? /].freeze
10
-
11
- ##
12
- # Validates configuration.
13
- #
14
- # @return [String] the generated password
15
- def self.validate_config!
16
- boolean_classes = [TrueClass, FalseClass]
17
-
18
- raise ConfigurationError, 'password_length: expected Integer' unless password_length.is_a?(Integer)
19
- raise ConfigurationError, 'has_lower_letters: expected Boolean' unless boolean_classes.include?(has_lower_letters.class)
20
- raise ConfigurationError, 'min_lower_letters: expected Integer' unless min_lower_letters.is_a?(Integer)
21
- raise ConfigurationError, 'has_upper_letters: expected Boolean' unless boolean_classes.include?(has_upper_letters.class)
22
- raise ConfigurationError, 'min_upper_letters: expected Integer' unless min_upper_letters.is_a?(Integer)
23
- raise ConfigurationError, 'has_numbers: expected Boolean' unless boolean_classes.include?(has_numbers.class)
24
- raise ConfigurationError, 'min_numbers: expected Integer' unless min_numbers.is_a?(Integer)
25
- raise ConfigurationError, 'has_symbols: expected Boolean' unless boolean_classes.include?(has_symbols.class)
26
- raise ConfigurationError, 'min_symbols: expected Integer' unless min_symbols.is_a?(Integer)
27
-
28
- raise ConfigurationError, 'No usable character set.' unless has_lower_letters || has_upper_letters || has_numbers || has_symbols
29
- end
1
+ require_relative './password_util/config'
2
+ require_relative './password_util/generator'
30
3
 
31
- ##
32
- # Resets configuration to default.
33
- def self.reset_config
34
- self.password_length = 8
35
- self.has_lower_letters = true
36
- self.min_lower_letters = 1
37
- self.has_upper_letters = true
38
- self.min_upper_letters = 1
39
- self.has_numbers = true
40
- self.min_numbers = 1
41
- self.has_symbols = true
42
- self.min_symbols = 1
43
- end
4
+ module PasswordUtil
5
+ singleton_class.attr_accessor :config
6
+ self.config = Config.new({})
7
+
8
+ class << self
9
+ ##
10
+ # Resets configuration to default.
11
+ #
12
+ def reset_config
13
+ self.config = Config.new({})
14
+ end
44
15
 
45
- ##
46
- # Generates password.
47
- #
48
- # @return [String] the generated password
49
- def self.generate
50
- validate_config!
51
- charset = []
52
- password = []
53
-
54
- if has_lower_letters
55
- charset << LOWER_LETTERS
56
- min_lower_letters.times do
57
- password << LOWER_LETTERS.sample
58
- end
16
+ ##
17
+ # Generates password.
18
+ #
19
+ # @return [String] the generated password
20
+ def generate
21
+ Generator.new(config).generate
59
22
  end
60
23
 
61
- if has_upper_letters
62
- charset << UPPER_LETTERS
63
- min_upper_letters.times do
64
- password << UPPER_LETTERS.sample
65
- end
24
+ def has_lower_letters=(value)
25
+ config.has_lower_letters = value
66
26
  end
67
27
 
68
- if has_numbers
69
- charset << NUMBERS
70
- min_numbers.times do
71
- password << NUMBERS.sample
72
- end
28
+ def has_numbers=(value)
29
+ config.has_numbers = value
73
30
  end
74
31
 
75
- if has_symbols
76
- charset << SYMBOLS
77
- min_symbols.times do
78
- password << SYMBOLS.sample
79
- end
32
+ def has_symbols=(value)
33
+ config.has_symbols = value
80
34
  end
81
35
 
82
- password.shuffle!
83
- password << charset.sample.sample while password.length < password_length
84
- password.join('')[0...password_length]
85
- end
36
+ def has_upper_letters=(value)
37
+ config.has_upper_letters = value
38
+ end
86
39
 
87
- singleton_class.attr_accessor :password_length
88
- self.password_length = 8
40
+ def lower_letters=(value)
41
+ config.lower_letters = value
42
+ end
89
43
 
90
- singleton_class.attr_accessor :has_lower_letters
91
- self.has_lower_letters = true
44
+ def min_lower_letters=(value)
45
+ config.min_lower_letters = value
46
+ end
92
47
 
93
- singleton_class.attr_accessor :min_lower_letters
94
- self.min_lower_letters = 1
48
+ def min_numbers=(value)
49
+ config.min_numbers = value
50
+ end
95
51
 
96
- singleton_class.attr_accessor :has_upper_letters
97
- self.has_upper_letters = true
52
+ def min_symbols=(value)
53
+ config.min_symbols = value
54
+ end
98
55
 
99
- singleton_class.attr_accessor :min_upper_letters
100
- self.min_upper_letters = 1
56
+ def min_upper_letters=(value)
57
+ config.min_upper_letters = value
58
+ end
101
59
 
102
- singleton_class.attr_accessor :has_numbers
103
- self.has_numbers = true
60
+ def numbers=(value)
61
+ config.numbers = value
62
+ end
104
63
 
105
- singleton_class.attr_accessor :min_numbers
106
- self.min_numbers = 1
64
+ def password_length=(value)
65
+ config.password_length = value
66
+ end
107
67
 
108
- singleton_class.attr_accessor :has_symbols
109
- self.has_symbols = true
68
+ def symbols=(value)
69
+ config.symbols = value
70
+ end
110
71
 
111
- singleton_class.attr_accessor :min_symbols
112
- self.min_symbols = 1
72
+ def upper_letters=(value)
73
+ config.upper_letters = value
74
+ end
75
+ end
113
76
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: password_util
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Reinier John Avila
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-11-01 00:00:00.000000000 Z
11
+ date: 2023-05-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Utility tool to generate password.
14
14
  email: reinieravila@gmail.com
@@ -19,6 +19,9 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - bin/password_util
21
21
  - lib/password_util.rb
22
+ - lib/password_util/character_sets.rb
23
+ - lib/password_util/config.rb
24
+ - lib/password_util/generator.rb
22
25
  homepage: https://github.com/reinieravila/password_util.git
23
26
  licenses:
24
27
  - MIT
@@ -38,7 +41,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
38
41
  - !ruby/object:Gem::Version
39
42
  version: '0'
40
43
  requirements: []
41
- rubygems_version: 3.3.7
44
+ rubygems_version: 3.4.1
42
45
  signing_key:
43
46
  specification_version: 4
44
47
  summary: Password Generation Utility Tool