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 +4 -4
- data/bin/password_util +17 -0
- data/lib/password_util/character_sets.rb +8 -0
- data/lib/password_util/config.rb +56 -0
- data/lib/password_util/generator.rb +50 -0
- data/lib/password_util.rb +56 -81
- metadata +11 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dfc4714b8ab73246626ae199a8d874ce89fbab223466529a3059a8a952b22a26
|
4
|
+
data.tar.gz: cb5de8dd65ecb31cd05fda34369cc97b979b94c66730743d49d4ee6b382ead8f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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,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
|
-
|
2
|
-
|
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
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
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
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
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
|
-
|
50
|
-
|
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
|
-
|
57
|
-
|
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
|
-
|
64
|
-
|
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
|
-
|
71
|
-
|
72
|
-
|
73
|
-
end
|
36
|
+
def has_upper_letters=(value)
|
37
|
+
config.has_upper_letters = value
|
38
|
+
end
|
74
39
|
|
75
|
-
|
76
|
-
|
40
|
+
def lower_letters=(value)
|
41
|
+
config.lower_letters = value
|
42
|
+
end
|
77
43
|
|
78
|
-
|
79
|
-
|
44
|
+
def min_lower_letters=(value)
|
45
|
+
config.min_lower_letters = value
|
46
|
+
end
|
80
47
|
|
81
|
-
|
82
|
-
|
48
|
+
def min_numbers=(value)
|
49
|
+
config.min_numbers = value
|
50
|
+
end
|
83
51
|
|
84
|
-
|
85
|
-
|
52
|
+
def min_symbols=(value)
|
53
|
+
config.min_symbols = value
|
54
|
+
end
|
86
55
|
|
87
|
-
|
88
|
-
|
56
|
+
def min_upper_letters=(value)
|
57
|
+
config.min_upper_letters = value
|
58
|
+
end
|
89
59
|
|
90
|
-
|
91
|
-
|
60
|
+
def numbers=(value)
|
61
|
+
config.numbers = value
|
62
|
+
end
|
92
63
|
|
93
|
-
|
94
|
-
|
64
|
+
def password_length=(value)
|
65
|
+
config.password_length = value
|
66
|
+
end
|
95
67
|
|
96
|
-
|
97
|
-
|
68
|
+
def symbols=(value)
|
69
|
+
config.symbols = value
|
70
|
+
end
|
98
71
|
|
99
|
-
|
100
|
-
|
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
|
+
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:
|
11
|
+
date: 2023-05-27 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
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.
|
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: []
|