generatepass 0.2.0 → 0.3.0

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: febd234112eae33670c326a250104ff9f02f6e9a7e6b6a0f8fbe6729ceafb4c3
4
- data.tar.gz: 405aa306cd6dc61fff114574a1744c1095dc82de370d1579572476aedcca900d
3
+ metadata.gz: 4fc6b86ce67f0546b09c98379c364ab8fe77571a67421e359e607bbad80dd660
4
+ data.tar.gz: f04a60e09e48d460294eb788fea61a7baa0db48786e3f26776d80813016b6442
5
5
  SHA512:
6
- metadata.gz: 8466759e49f38d75a4c63b275de591c374f5e2feb700fa71dfddd225a11f5a8250af8c661e3e92aebf6bb4640f3ae7f20a0fe7caf7c506b10325637afd9da7a5
7
- data.tar.gz: 16c97d26843f9ff58f2151f3666e7177bf6a3be4c637d4832db1a9408827e2f33dc24c93d10de472242d31c3b5bda5273d8492000492ca4bae366063258866ff
6
+ metadata.gz: 68034190a662541f1bed4b266a144c754e49804bd8ac749ee704de0653f29b147f416eb6edea938a121d75b6eb826da3b5cc4c497694fef8c39c66346ede537a
7
+ data.tar.gz: 2d1e9656494244504fdffab529087de8710b09b518f6975c0bf1f3f0f4596b8fbe73fd84d15f9201edb32a2e384504314cb761e61d7713cb27fe8f08650590bf
data/README.md CHANGED
@@ -1,24 +1,95 @@
1
1
  # Generatepass
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
4
-
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/generatepass`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ Just another gem to generate random passwords and tokens, maybe useful for somebody :)
6
4
 
7
5
  ## Installation
8
6
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
-
11
7
  Install the gem and add to the application's Gemfile by executing:
12
8
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
9
+ $ bundle add generatepass
14
10
 
15
11
  If bundler is not being used to manage dependencies, install the gem by executing:
16
12
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
13
+ $ gem install generatepass
18
14
 
19
15
  ## Usage
20
16
 
21
- TODO: Write usage instructions here
17
+ ### Generate a random password
18
+
19
+ Optional params:
20
+
21
+ - length [Integer] The length of the password. Defaults to 12.
22
+ - include_uppercase [Boolean] Whether to include uppercase letters. Defaults to true.
23
+ - include_symbols [Boolean] Whether to include symbols. Defaults to true.
24
+ - include_digits [Boolean] Whether to include digits. Defaults to true.
25
+ - exclude_ambiguous [Boolean] Whether to exclude ambiguous characters. Defaults to false.
26
+ - url_safe [Boolean] Whether the password should be URL safe. Defaults to false.
27
+
28
+
29
+ ```
30
+ require 'generatepass'
31
+
32
+ Generatepass.gen
33
+ => "Qwertyuiop"
34
+
35
+ Generatepass.gen(length: 20)
36
+ => "Qwertyuiopasdfghjklzxcvbnm1234567890"
37
+
38
+ Generatepass.gen(include_uppercase: false)
39
+ => "xl6#_q.+,sis"
40
+
41
+ Generatepass.gen(include_symbols: false)
42
+ => "BSaF10pfaJty"
43
+
44
+ Generatepass.gen(include_digits: false)
45
+ => "C:&!}E&UZ&am"
46
+
47
+ Generatepass.gen(exclude_ambiguous: true)
48
+ => "VWs9]Qx77&}*"
49
+
50
+ Generatepass.gen(url_safe: true)
51
+ => "LYLqE,N+(&Jm"
52
+ ```
53
+
54
+ ### Generate a random hexadecimal token
55
+
56
+ ```
57
+ require 'generatepass'
58
+
59
+ Generatepass.token_hex
60
+ => "5e22d42c3e8a6509cd873f0cfdbc53b2d5f4b0c2ef695a4c8442a2e7b7ea7a79"
61
+ ```
62
+
63
+ ### Generate a random a random Base64 Token
64
+
65
+ Optional params:
66
+ - url_safe [Boolean] Whether the token should be URL safe. Defaults to false.
67
+
68
+ ```
69
+ require 'generatepass'
70
+
71
+ Generatepass.token_base64
72
+ => "2ok5bRvyKEYWIOSLrsa4qzXGk7dadWwUZXBinWguhSE="
73
+
74
+ Generatepass.token_base64(url_safe: true)
75
+ => "GSJ6nTJ96Js9pn6CEh54VJplcFCK64BZHnc3lBO8HI"
76
+ ```
77
+
78
+ ### Generate a random a random Base64 Token (version 2)
79
+
80
+ Optional params:
81
+ - url_safe [Boolean] Whether the token should be URL safe. Defaults to false.
82
+
83
+ ```
84
+ require 'generatepass'
85
+
86
+ Generatepass.token_base64b
87
+ => "MTcyNTQxMzQ2OTgxNTc0NjIwMTY1ODYzMDk=\n"
88
+
89
+ Generatepass.token_base64b(url_safe: true)
90
+ => "MTcyNTQxMzQ1ODUzNjAyMDg3NjY0NTYwMzk"
91
+ ```
92
+
22
93
 
23
94
  ## Development
24
95
 
@@ -28,7 +99,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
28
99
 
29
100
  ## Contributing
30
101
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/generatepass. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/generatepass/blob/master/CODE_OF_CONDUCT.md).
102
+ Bug reports and pull requests are welcome on GitHub at https://github.com/angelpadilla/Generatepass. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/angelpadilla/Generatepass/blob/master/CODE_OF_CONDUCT.md).
32
103
 
33
104
  ## License
34
105
 
@@ -36,4 +107,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
36
107
 
37
108
  ## Code of Conduct
38
109
 
39
- Everyone interacting in the Generatepass project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/generatepass/blob/master/CODE_OF_CONDUCT.md).
110
+ Everyone interacting in the Generatepass project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/angelpadilla/Generatepass/blob/master/CODE_OF_CONDUCT.md).
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Generatepass
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/generatepass.rb CHANGED
@@ -14,10 +14,57 @@ module Generatepass
14
14
  URL_UNSAFE = "=#%/:@&?".split(//)
15
15
  AMBIGUOUS = "B8G6I1l0OQDS5Z2Ss5Bb8|Iio01lO".split(//)
16
16
 
17
+ # Generates a password with the specified length and options.
18
+ #
19
+ # @param length [Integer] The length of the password. Defaults to 12.
20
+ # @param include_uppercase [Boolean] Whether to include uppercase letters. Defaults to true.
21
+ # @param include_symbols [Boolean] Whether to include symbols. Defaults to true.
22
+ # @param include_digits [Boolean] Whether to include digits. Defaults to true.
23
+ # @param exclude_ambiguous [Boolean] Whether to exclude ambiguous characters. Defaults to false.
24
+ # @param url_safe [Boolean] Whether the password should be URL safe. Defaults to false.
25
+ #
26
+ # @return [String] The generated password.
27
+ #
28
+ # @raise [ArgumentError] If the password length is less than 5.
29
+ #
30
+ # @example
31
+ # Generatepass.gen
32
+ # # => "Qwertyuiop"
33
+ # Generatepass.gen(length: 20)
34
+ # # => "Qwertyuiopasdfghjklzxcvbnm1234567890"
35
+ # Generatepass.gen(include_uppercase: false)
36
+ # # => "xl6#_q.+,sis"
37
+ # Generatepass.gen(include_symbols: false)
38
+ # # => "BSaF10pfaJty"
39
+ # Generatepass.gen(include_digits: false)
40
+ # # => "C:&!}E&UZ&am"
41
+ # Generatepass.gen(exclude_ambiguous: true)
42
+ # # => "VWs9]Qx77&}*"
43
+ # Generatepass.gen(url_safe: true)
44
+ # # => "LYLqE,N+(&Jm"
45
+ #
17
46
  def self.gen(length: 12, include_uppercase: true, include_symbols: true, include_digits: true, exclude_ambiguous: false, url_safe: false)
18
- # puts Digest::SHA256.hexdigest("#{Time.now}#{rand}")
19
- # puts Digest::SHA256.hexdigest("#{Time.now}#{rand}").to_i(16).to_s(36)
20
- raise "Minimum length is 5" if length < 5
47
+ raise ArgumentError, "Minimum password length is 5" if length < 5
48
+ raise ArgumentError, ":length must be an integer" if length.class != Integer
49
+ raise ArgumentError, ":length must be greater than 0" if length <= 0
50
+
51
+ if include_uppercase.class != TrueClass && include_uppercase.class != FalseClass
52
+ raise ArgumentError,
53
+ ":include_uppercase must be a boolean"
54
+ end
55
+ if include_symbols.class != TrueClass && include_symbols.class != FalseClass
56
+ raise ArgumentError,
57
+ ":include_symbols must be a boolean"
58
+ end
59
+ if include_digits.class != TrueClass && include_digits.class != FalseClass
60
+ raise ArgumentError,
61
+ ":include_digits must be a boolean"
62
+ end
63
+ if exclude_ambiguous.class != TrueClass && exclude_ambiguous.class != FalseClass
64
+ raise ArgumentError,
65
+ ":exclude_ambiguous must be a boolean"
66
+ end
67
+ raise ArgumentError, ":url_safe must be a boolean" if url_safe.class != TrueClass && url_safe.class != FalseClass
21
68
 
22
69
  charset = LOWERCASE
23
70
  charset += UPPERCASE if include_uppercase
@@ -29,31 +76,65 @@ module Generatepass
29
76
  (0...length).map { charset.sample }.join
30
77
  end
31
78
 
79
+ # Generates a token in hexadecimal format using SHA-256 hashing, useful for password or api tokens.
80
+ #
81
+ # @return [String] A hexadecimal string representing the token.
82
+ #
83
+ # @example
84
+ # Generatepass.token_hex
85
+ # # => "5e22d42c3e8a6509cd873f0cfdbc53b2d5f4b0c2ef695a4c8442a2e7b7ea7a79"
32
86
  def self.token_hex
33
- rand1 = rand(99_999_999).to_s.rjust(8, "0")
34
- rand2 = rand(99_999_999).to_s.rjust(8, "0")
35
- Digest::SHA256.hexdigest("#{Time.now.to_i}#{rand1}#{rand2}")
87
+ random_number1 = rand(99_999_999).to_s.rjust(8, "0")
88
+ random_number2 = rand(99_999_999).to_s.rjust(8, "0")
89
+ Digest::SHA256.hexdigest("#{Time.now.to_i}#{random_number1}#{random_number2}")
36
90
  end
37
91
 
92
+ # Generates a token in base64 format using SHA-256 hashing, useful for password or api tokens.
93
+ #
94
+ # @param url_safe [Boolean] Whether the token should be URL safe. Defaults to false.
95
+ #
96
+ # @return [String] A base64 string representing the token.
97
+ #
98
+ # @example
99
+ # Generatepass.token_base64
100
+ # # => "2ok5bRvyKEYWIOSLrsa4qzXGk7dadWwUZXBinWguhSE="
101
+ # Generatepass.token_base64(url_safe: true)
102
+ # # => "GSJ6nTJ96Js9pn6CEh54VJplcFCK64BZHnc3lBO8HI"
38
103
  def self.token_base64(url_safe: false)
39
- rand1 = rand(99_999_999).to_s.rjust(8, "0")
40
- rand2 = rand(99_999_999).to_s.rjust(8, "0")
104
+ raise ArgumentError, ":url_safe must be a boolean" if url_safe.class != TrueClass && url_safe.class != FalseClass
105
+
106
+ random_number1 = rand(99_999_999).to_s.rjust(8, "0")
107
+ random_number2 = rand(99_999_999).to_s.rjust(8, "0")
108
+ digest = Digest::SHA256.base64digest("#{Time.now.to_i}#{random_number1}#{random_number2}")
109
+
41
110
  if url_safe
42
- set = Digest::SHA256.base64digest("#{Time.now.to_i}#{rand1}#{rand2}").split(//) - URL_UNSAFE
43
- set -= URL_UNSAFE
111
+ set = digest.split(//) - URL_UNSAFE
44
112
  set.join
45
113
  else
46
- Digest::SHA256.base64digest("#{Time.now.to_i}#{rand1}#{rand2}")
114
+ digest
47
115
  end
48
116
  end
49
117
 
118
+ # Generates a token in base64 (version 'b') format using Base64 encoding, useful for password or api tokens.
119
+ #
120
+ # @param url_safe [Boolean] Whether the token should be URL safe. Defaults to false.
121
+ #
122
+ # @return [String] A base64 string representing the token.
123
+ #
124
+ # @example
125
+ # Generatepass.token_base64b
126
+ # # => "MTcyNTQxMzQ2OTgxNTc0NjIwMTY1ODYzMDk=\n"
127
+ # Generatepass.token_base64b(url_safe: true)
128
+ # # => "MTcyNTQxMzQ1ODUzNjAyMDg3NjY0NTYwMzk"
50
129
  def self.token_base64b(url_safe: false)
51
- rand1 = rand(99_999_999).to_s.rjust(8, "0")
52
- rand2 = rand(99_999_999).to_s.rjust(8, "0")
130
+ raise ArgumentError, ":url_safe must be a boolean" if url_safe.class != TrueClass && url_safe.class != FalseClass
131
+
132
+ random_number1 = rand(99_999_999).to_s.rjust(8, "0")
133
+ random_number2 = rand(99_999_999).to_s.rjust(8, "0")
53
134
  if url_safe
54
- Base64.urlsafe_encode64("#{Time.now.to_i}#{rand1}#{rand2}", padding: false)
135
+ Base64.urlsafe_encode64("#{Time.now.to_i}#{random_number1}#{random_number2}", padding: false)
55
136
  else
56
- Base64.encode64("#{Time.now.to_i}#{rand1}#{rand2}")
137
+ Base64.encode64("#{Time.now.to_i}#{random_number1}#{random_number2}")
57
138
  end
58
139
  end
59
140
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: generatepass
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Angel Padilla
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-08-09 00:00:00.000000000 Z
11
+ date: 2024-09-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Just another password generator
14
14
  email:
@@ -35,7 +35,7 @@ metadata:
35
35
  homepage_uri: https://github.com/angelpadilla/Generatepass
36
36
  source_code_uri: https://github.com/angelpadilla/Generatepass
37
37
  changelog_uri: https://github.com/angelpadilla/Generatepass/blob/master/CHANGELOG.md
38
- post_install_message:
38
+ post_install_message:
39
39
  rdoc_options: []
40
40
  require_paths:
41
41
  - lib
@@ -50,8 +50,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
50
50
  - !ruby/object:Gem::Version
51
51
  version: '0'
52
52
  requirements: []
53
- rubygems_version: 3.5.17
54
- signing_key:
53
+ rubygems_version: 3.5.11
54
+ signing_key:
55
55
  specification_version: 4
56
56
  summary: Another password generator
57
57
  test_files: []