password_util 0.0.4 → 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: 585b4336af1d3cc4191a8f5ae5e5fa9f820dcc28aaaa6e4c4d90215436825c51
4
- data.tar.gz: c553b39e03316a0882d05daac8463b8c75779ab2ef2dc99a4bc27d84cf8cb33f
3
+ metadata.gz: dfc4714b8ab73246626ae199a8d874ce89fbab223466529a3059a8a952b22a26
4
+ data.tar.gz: cb5de8dd65ecb31cd05fda34369cc97b979b94c66730743d49d4ee6b382ead8f
5
5
  SHA512:
6
- metadata.gz: 3cd8c25a9a87be821e22fd6eaf68ba8742531c44a955f4ad69c3036da41359f1f6e91afc987ca4b9d2d8b290047962a9e695232689f8d1a30268622c55ed54fe
7
- data.tar.gz: d8fa4ca0218c83b99d321328f9b35e08c1c89330c32f39163a7d735e30f3ace975ac01ca18000f7bc474456356950b9b3e0a46522495bcb1a4a9c3c7eb22d0c4
6
+ metadata.gz: 1858630fc1d0441bc54a4da9836165922480dcb776287c5602856b86692f30fd1163fa325a42b747e22421be47c02762ecf2f58026f83ff52fc744fcbb200e39
7
+ data.tar.gz: 7af824a87c53c09e64cc3e4125d2cd70fceb3c8a7bd49290eff0cc08b128ec8026f62a6f356ea67bc0017ff5bf82f75816b3e42682ee6dd19bc03d96c5a74fd7
data/bin/password_util ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'password_util'
4
+
5
+ password_length = ENV['LENGTH']
6
+ has_upper_letters = ENV['UPPER_LETTERS']
7
+ has_lower_letters = ENV['LOWER_LETTERS']
8
+ has_numbers = ENV['NUMBERS']
9
+ has_symbols = ENV['SYMBOLS']
10
+
11
+ PasswordUtil.password_length = password_length.to_i if password_length.to_s =~ /\A\d+\z/
12
+ PasswordUtil.has_upper_letters = has_upper_letters == 1 if has_upper_letters.to_s =~ /\A(?:0|1)\z/
13
+ PasswordUtil.has_lower_letters = has_lower_letters == 1 if has_lower_letters.to_s =~ /\A(?:0|1)\z/
14
+ PasswordUtil.has_numbers = has_numbers == 1 if has_numbers.to_s =~ /\A(?:0|1)\z/
15
+ PasswordUtil.has_symbols = has_symbols == 1 if has_symbols.to_s =~ /\A(?:0|1)\z/
16
+
17
+ puts PasswordUtil.generate
@@ -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,101 +1,76 @@
1
- module PasswordUtil
2
- class ConfigurationError < StandardError; end
3
-
4
- UPPER_LETTERS = ('A'..'Z').to_a.freeze
5
- LOWER_LETTERS = ('a'..'z').to_a.freeze
6
- NUMBERS = ('0'..'9').to_a.freeze
7
- SYMBOLS = %w[~ ` ! @ # $ % ^ & * ( ) _ - + = { \[ } \] | \\ : ; " ' < , > . ? /].freeze
8
-
9
- def self.validate_config!
10
- boolean_classes = [TrueClass, FalseClass]
11
-
12
- raise ConfigurationError, 'password_length: expected Integer' unless password_length.is_a?(Integer)
13
- raise ConfigurationError, 'has_lower_letters: expected Boolean' unless boolean_classes.include?(has_lower_letters.class)
14
- raise ConfigurationError, 'min_lower_letters: expected Integer' unless min_lower_letters.is_a?(Integer)
15
- raise ConfigurationError, 'has_upper_letters: expected Boolean' unless boolean_classes.include?(has_upper_letters.class)
16
- raise ConfigurationError, 'min_upper_letters: expected Integer' unless min_upper_letters.is_a?(Integer)
17
- raise ConfigurationError, 'has_numbers: expected Boolean' unless boolean_classes.include?(has_numbers.class)
18
- raise ConfigurationError, 'min_numbers: expected Integer' unless min_numbers.is_a?(Integer)
19
- raise ConfigurationError, 'has_symbols: expected Boolean' unless boolean_classes.include?(has_symbols.class)
20
- raise ConfigurationError, 'min_symbols: expected Integer' unless min_symbols.is_a?(Integer)
21
-
22
- raise ConfigurationError, 'No usable character set.' unless has_lower_letters || has_upper_letters || has_numbers || has_symbols
23
- end
1
+ require_relative './password_util/config'
2
+ require_relative './password_util/generator'
24
3
 
25
- def self.reset_config
26
- self.password_length = 8
27
- self.has_lower_letters = true
28
- self.min_lower_letters = 1
29
- self.has_upper_letters = true
30
- self.min_upper_letters = 1
31
- self.has_numbers = true
32
- self.min_numbers = 1
33
- self.has_symbols = true
34
- self.min_symbols = 1
35
- end
36
-
37
- def self.generate
38
- validate_config!
39
- charset = []
40
- password = []
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
41
15
 
42
- if has_lower_letters
43
- charset << LOWER_LETTERS
44
- min_lower_letters.times do
45
- password << LOWER_LETTERS.sample
46
- end
16
+ ##
17
+ # Generates password.
18
+ #
19
+ # @return [String] the generated password
20
+ def generate
21
+ Generator.new(config).generate
47
22
  end
48
23
 
49
- if has_upper_letters
50
- charset << UPPER_LETTERS
51
- min_upper_letters.times do
52
- password << UPPER_LETTERS.sample
53
- end
24
+ def has_lower_letters=(value)
25
+ config.has_lower_letters = value
54
26
  end
55
27
 
56
- if has_numbers
57
- charset << NUMBERS
58
- min_numbers.times do
59
- password << NUMBERS.sample
60
- end
28
+ def has_numbers=(value)
29
+ config.has_numbers = value
61
30
  end
62
31
 
63
- if has_symbols
64
- charset << SYMBOLS
65
- min_symbols.times do
66
- password << SYMBOLS.sample
67
- end
32
+ def has_symbols=(value)
33
+ config.has_symbols = value
68
34
  end
69
35
 
70
- password.shuffle!
71
- password << charset.sample.sample while password.length < password_length
72
- password.join('')[0...password_length]
73
- end
36
+ def has_upper_letters=(value)
37
+ config.has_upper_letters = value
38
+ end
74
39
 
75
- singleton_class.attr_accessor :password_length
76
- self.password_length = 8
40
+ def lower_letters=(value)
41
+ config.lower_letters = value
42
+ end
77
43
 
78
- singleton_class.attr_accessor :has_lower_letters
79
- self.has_lower_letters = true
44
+ def min_lower_letters=(value)
45
+ config.min_lower_letters = value
46
+ end
80
47
 
81
- singleton_class.attr_accessor :min_lower_letters
82
- self.min_lower_letters = 1
48
+ def min_numbers=(value)
49
+ config.min_numbers = value
50
+ end
83
51
 
84
- singleton_class.attr_accessor :has_upper_letters
85
- self.has_upper_letters = true
52
+ def min_symbols=(value)
53
+ config.min_symbols = value
54
+ end
86
55
 
87
- singleton_class.attr_accessor :min_upper_letters
88
- self.min_upper_letters = 1
56
+ def min_upper_letters=(value)
57
+ config.min_upper_letters = value
58
+ end
89
59
 
90
- singleton_class.attr_accessor :has_numbers
91
- self.has_numbers = true
60
+ def numbers=(value)
61
+ config.numbers = value
62
+ end
92
63
 
93
- singleton_class.attr_accessor :min_numbers
94
- self.min_numbers = 1
64
+ def password_length=(value)
65
+ config.password_length = value
66
+ end
95
67
 
96
- singleton_class.attr_accessor :has_symbols
97
- self.has_symbols = true
68
+ def symbols=(value)
69
+ config.symbols = value
70
+ end
98
71
 
99
- singleton_class.attr_accessor :min_symbols
100
- self.min_symbols = 1
72
+ def upper_letters=(value)
73
+ config.upper_letters = value
74
+ end
75
+ end
101
76
  end
metadata CHANGED
@@ -1,22 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: password_util
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
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
- description: Different password related utility tool.
13
+ description: Utility tool to generate password.
14
14
  email: reinieravila@gmail.com
15
- executables: []
15
+ executables:
16
+ - password_util
16
17
  extensions: []
17
18
  extra_rdoc_files: []
18
19
  files:
20
+ - bin/password_util
19
21
  - lib/password_util.rb
22
+ - lib/password_util/character_sets.rb
23
+ - lib/password_util/config.rb
24
+ - lib/password_util/generator.rb
20
25
  homepage: https://github.com/reinieravila/password_util.git
21
26
  licenses:
22
27
  - MIT
@@ -36,8 +41,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
36
41
  - !ruby/object:Gem::Version
37
42
  version: '0'
38
43
  requirements: []
39
- rubygems_version: 3.3.7
44
+ rubygems_version: 3.4.1
40
45
  signing_key:
41
46
  specification_version: 4
42
- summary: Password Utility Tool
47
+ summary: Password Generation Utility Tool
43
48
  test_files: []