randpass 0.1.3 → 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: 56fe4a3d0922ef73687911332685af59657369380b77514e533ede43854cee86
4
- data.tar.gz: 6f9adf346eb9fc6339964201aee71563ec41ce7b3c68b8e4a82fbaa90c94e924
3
+ metadata.gz: ff3fc4f874b2494d17acd4781ead4b775d83f5498258e77ccea97bfeae90ab00
4
+ data.tar.gz: 015f7ae1c0567a0c403e31447c6f2f4ce63342128a9e9da6b41da95c3930ef22
5
5
  SHA512:
6
- metadata.gz: 938d2f69909daca1f9488c8ea23837a29caa25a995039a7b526e6a1d9c5ce66035fde1a62135544f52950028e55c952cc8b8aa077f8ed4b68d9521a5c914ba15
7
- data.tar.gz: b7ec2a62282de7423cc955b4073fd0acf9a43dc7494721a6bc7b111a753557de8b5bac6b9b90e71a947f633f5285bc579549329a139b3e8b554d6040d568dc5e
6
+ metadata.gz: 9a6e5995afa0b9eb6ffa5b0973587f925b2989a1beed08d7a2bd1681266288c18a06d146964f829fd5f9b6acdacb6768c2e47adacb5d15f1bbccb98c607d0d60
7
+ data.tar.gz: 8320a134f57d210ee2e47e87571dfa3937e903d9c8dd2c0225e017a2d89cbdd695bc07cf3c5440b5eadbc148c09a868bdb73a146f14322d08a0069306fd1ab49
data/README.md CHANGED
@@ -1,71 +1,83 @@
1
- # Randpass
1
+ # Randpass v0.3.0
2
+
3
+ Ruby random password generator.
4
+ Generate password and copy in clipboard, or generate list and/or save it as `.txt` file.
5
+ File is saved as plain text, it is up to end-user to decide how to secure it.
6
+
7
+ Password is generated with `SecureRandom#base64` and a few (random) special characters `! # * $ % _ @`
8
+
9
+
10
+ ## Dependencies
11
+
12
+ Randpass use _clipboard-gem_, that require `xsel` or `xclip` on Linux systems.
13
+ Setup script will check and install `xsel` if nothing is found.
14
+
15
+
16
+ ## How to use:
17
+
18
+ Use from terminal:
19
+
20
+ When generating a single password, you don't need any argument, or you can add number of password characters (default is 25). By default, password is printed in STDOUT and copied to clipboard, but not saved as file.
21
+
22
+ - generate password with 25 characters
23
+ `randpass`
24
+
25
+ - generate password with 30 characters
26
+ `randpass 30`
27
+ `randpass --char 30`
28
+
29
+ - clear clipboard
30
+ `randpass -k`
31
+ `randpass --clear`
32
+
33
+ - add `-c` or `--noc` to disable clipboard copy
34
+ `randpass 35 -c`
35
+
36
+ - add `-p` or `--nop` to disable STDOUT
37
+ `randpass 20 -p`
38
+
39
+ When generating a list of passwords, you need to supply `-n` or `--char` to set number of password characters (default is 25). Passwords will be saved in a current working directory (unless otherwise specified) in format `randpass_#{Time.now.to_i}.txt`. This way we avoid overwrite, and it's possible to get time of file generation with `Time.at TIME_INTEGER`
40
+
41
+ - generate password list from comments (20 characters long password)
42
+ `randpass -n 20 -l GitHub RubyGems AnotherWebSite`
43
+
44
+ - generate password list with 10 passwords - without comments
45
+ `randpass -r 10`
46
+ `randpass -n 20 -r 10`
47
+
48
+ - add `-s` or `--nos` to disable saving list as a file
49
+ `randpass -n 20 -r 5 -s`
50
+ `randpass -r 10 --nos --noc`
51
+
52
+ - add `-d` or `--dir` as location to store password list
53
+ `randpass -r 10 -d 'my_path/directory'`
54
+
2
55
 
3
- Create random password with ruby. Use `SecureRandom#base64` + few random special characters.
4
56
 
5
57
  ## How to install
6
58
 
7
59
  - install from rubygems
8
60
 
9
61
  ```
10
- gem install randpass
62
+ gem install randpass && randpass -i
11
63
  ```
12
64
  - download from github with ssh
13
65
 
14
66
  ```
15
67
  git clone git@github.com:alx3dev/randpass \
16
- cd randpass && bundle install
68
+ cd randpass && randpass -i
17
69
  ```
18
70
  - download from github with https
19
71
 
20
72
  ```
21
73
  git clone https://www.github.com/alx3dev/randpass \
22
- cd randpass && bundle install
23
- ```
24
-
25
- To build your own gem, run `rake bundle`, and install it locally with `gem install pkg/randpass-0.1.3.gem`
74
+ cd randpass && randpass -i
75
+ ```
26
76
 
27
- ## How to use:
28
-
29
- - use from terminal
30
-
31
- ```
32
- # default 22 characters
33
- randpass
34
- => W4fkGVdXx6pzk$O?VP11!wWs
35
-
36
- # or add number of characters as argument
37
- randpass 33
38
- => etkK$YW1R_PXK8FsjnGHr+%w2cTBbMTOTej3s8je?2ya
39
-
40
- # install from rubygems, or build your own version, otherwise you need to run:
41
- bin/randpass
42
- ```
43
- - use as library
44
-
45
- ```ruby
46
- require 'randpass'
47
-
48
- Randpass[20]
49
- # or
50
- Randpass.randpass 20
51
- => "1L3Jk$S850Np=ikQ7zeqb44qaBC9"
52
- ```
53
-
54
- Randpass is a module with both class and instance methods `#randpass`, so you can include/extend it in your class.
55
-
56
- ```ruby
57
- require 'randpass'
58
- include Randpass
59
-
60
- class MyClass
61
- def some_method
62
- random_string = randpass 33
63
- end
64
- end
65
- ```
66
77
 
67
78
  Tested on:
68
79
  - ruby `2.7.5`
69
80
  - ruby `3.0.3`
70
- - ruby `3.1.0-preview1`
81
+ - ruby `3.1.0`
82
+ - ruby `3.1.3`
71
83
  - jruby `9.3.2.0`
data/bin/output.rb ADDED
@@ -0,0 +1,78 @@
1
+ require_relative '../lib/randpass/version' unless defined? Randpass::VERSION
2
+
3
+ require 'optimist'
4
+
5
+ # Check if password should be printed and/or copied
6
+ module Output
7
+ # @param [String] pass **Required**. Password
8
+ def self.[](txt)
9
+ txt = txt.join("\n") if txt.is_a?(Array)
10
+ STDOUT.puts(txt) unless Randpass.noprint?
11
+ Clipboard.copy(txt) unless Randpass.nocopy?
12
+ end
13
+ end
14
+
15
+ BANNER =
16
+ <<~DESC
17
+ randpass
18
+ randpass 33
19
+ randpass -n 25 -r 5 --dir 'output/directory'
20
+ :
21
+ DESC
22
+
23
+ SETUP_DESCRIPTION =
24
+ <<~DESC
25
+ Install gem dependencies
26
+ xsel or xclip on linux (for clipboard manipulation)
27
+ DESC
28
+
29
+ NUMBER_DESCRIPTION =
30
+ <<~DESC
31
+ Number of password characters
32
+ randpass -n 20 -l github rubygems
33
+
34
+ ----------------------------------
35
+ DESC
36
+
37
+ LIST_DESCRIPTION =
38
+ <<~DESC
39
+ Save list of passwords for given comments
40
+ randpass -l GitHub GitLab StackOverflow
41
+
42
+ ----------------------------------
43
+ DESC
44
+
45
+ RANDOM_DESCRIPTION =
46
+ <<~DESC
47
+ Save list of passwords (number of passwords to generate)
48
+ randpass -r 5
49
+ randpass -r 5 -n 25
50
+
51
+ ----------------------------------
52
+ DESC
53
+
54
+ PATH_DESCRIPTION =
55
+ <<~DESC
56
+ Path to directory where to save password list
57
+ default: gem directory
58
+ DESC
59
+
60
+ CLIPBOARD_DESCRIPTION =
61
+ <<~DESC
62
+ Empty clipboard
63
+ DESC
64
+
65
+ NO_COPY_DESCRIPTION =
66
+ <<~DESC
67
+ Do not copy password to clipboard
68
+ DESC
69
+
70
+ NO_PRINT_DESCRIPTION =
71
+ <<~DESC
72
+ Do not print output to stdout
73
+ DESC
74
+
75
+ NO_SAVE_DESCRIPTION =
76
+ <<~DESC
77
+ Do not save list output as txt file
78
+ DESC
data/bin/randpass CHANGED
@@ -1,14 +1,67 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- require_relative '../lib/randpass'
4
+ require_relative 'output'
5
+ require_relative 'setup_gem'
5
6
 
6
- pass = if ARGV.empty?
7
- Randpass[22]
8
- else
9
- Randpass.randpass ARGV[0].to_i
10
- end
7
+ opts = Optimist.options do
8
+ banner BANNER
11
9
 
12
- # @todo Add clipboard gem
13
- # Clipboard.copy pass
14
- puts pass
10
+ opt :num, NUMBER_DESCRIPTION, short: 'n', long: 'char', type: :integer
11
+ opt :list, LIST_DESCRIPTION, short: 'l', long: 'list', type: :strings
12
+ opt :random, RANDOM_DESCRIPTION, short: 'r', long: 'random', type: :integer
13
+ opt :path, PATH_DESCRIPTION, short: 'd', long: 'dir', type: :string
14
+ opt :clear, CLIPBOARD_DESCRIPTION, short: 'k', long: 'clear', default: false
15
+ opt :nocopy, NO_COPY_DESCRIPTION, short: 'c', long: 'noc', default: false
16
+ opt :noprint, NO_PRINT_DESCRIPTION, short: 'p', long: 'nop', default: false
17
+ opt :nosave, NO_SAVE_DESCRIPTION, short: 's', long: 'nos', default: false
18
+
19
+ opt :setup, SETUP_DESCRIPTION, short: 'i', long: 'gem-install'
20
+
21
+ version "Randpass version #{Randpass::VERSION}"
22
+ end
23
+
24
+ if opts[:setup]
25
+
26
+ UserOS.gem_init
27
+ exit(1)
28
+
29
+ else
30
+ require_relative '../lib/randpass'
31
+ require 'clipboard'
32
+
33
+ (Clipboard.clear && exit(1)) if opts[:clear]
34
+
35
+ Randpass.path = opts[:path] if opts[:path]
36
+
37
+ Randpass.nocopy! if opts[:nocopy]
38
+ Randpass.nosave! if opts[:nosave]
39
+ Randpass.noprint! if opts[:noprint]
40
+
41
+ if opts[:list] && opts[:num]
42
+ Randpass.generate_list opts[:num].to_i, comments: opts[:list]
43
+ Output[Randpass.storage]
44
+
45
+ elsif opts[:list]
46
+ Randpass.generate_list nil, comments: opts[:list]
47
+ Output[Randpass.storage]
48
+
49
+ elsif opts[:num] && opts[:random]
50
+ Randpass.generate_list opts[:num], list: opts[:random]
51
+ Output[Randpass.storage]
52
+
53
+ elsif opts[:random]
54
+ Randpass.generate_list nil, list: opts[:random]
55
+ Output[Randpass.storage]
56
+
57
+ elsif opts[:num]
58
+ Output[Randpass[opts[:num]]]
59
+
60
+ elsif ARGV.empty?
61
+ Output[Randpass[20]]
62
+
63
+ elsif ARGV.size == 1
64
+ Output[Randpass[ARGV[0].to_i]]
65
+ end
66
+
67
+ end # if opts[:setup]
data/bin/setup ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ function install_clipboard_dependency() {
6
+ if !(command -v xsel) >/dev/null 2>&1; then
7
+ echo "Xsel was not found on the system..."
8
+
9
+ if !(command -v xclip) >/dev/null 2>&1; then
10
+ echo "Xclip also not found, installing Xsell..."
11
+ sudo apt update -y -qq
12
+ sudo apt install xsel -y
13
+
14
+ else
15
+ echo "but Xclip was"
16
+ fi
17
+
18
+ else
19
+ echo "Xsel found on the system"
20
+ fi
21
+ }
22
+
23
+ install_clipboard_dependency
24
+ bundle install
data/bin/setup_gem.rb ADDED
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Helper module for gem installation
4
+ module UserOS
5
+ GEM_ROOT = File.expand_path(__FILE__).gsub('/bin/setup_gem.rb', '')
6
+
7
+ class << self
8
+ def windows?
9
+ (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
10
+ end
11
+
12
+ def mac?
13
+ (/darwin/ =~ RUBY_PLATFORM) != nil
14
+ end
15
+
16
+ def linux?
17
+ !windows? && !mac?
18
+ end
19
+
20
+ # Call setup script based on user OS, if randpass is not installed
21
+ def gem_init
22
+ if system('bin/randpass --version')
23
+ puts 'was found on the system'
24
+ return
25
+ end
26
+ Dir.chdir GEM_ROOT
27
+ linux? ? system('bin/setup') : system('bundle install')
28
+ end
29
+ end
30
+ end
@@ -2,32 +2,21 @@
2
2
 
3
3
  require 'securerandom'
4
4
 
5
- # Generate random password with SecureRandom#base64 and a few
6
- # random special characters. Method #randpass is defined as
7
- # class and instance method, so you can call it or include it.
8
- #
9
5
  module Randpass
10
- # Allowed specials. xxx is just a flag for #add_special_chars
6
+ # Allowed specials (xxx is just a skip-flag)
11
7
  ARRAY = %w[! # * $ % _ @ xxx].freeze
12
8
 
13
- # Random number of times try to add random special character.
14
- # Transform to array, shuffle, and join back to string.
15
- #
16
- # Provide number of characters for password as argument,
17
- # default value is **22**.
18
- #
19
- # @param [Integer] number_of_chars Optional. Number of password characters.
20
- # @return [String]
21
- #
22
- def randpass(number_of_chars = nil)
23
- Randpass[number_of_chars]
24
- end
25
-
26
9
  class << self
10
+ # Generate random password
11
+ #
12
+ # @param [Integer] chars_no Optional. Number of password characters. Default: 25.
27
13
  # @return [String]
28
- def randpass(number_of_chars = 20)
29
- # slice string made with base64, to number of characters we want
30
- param = SecureRandom.base64(number_of_chars)[0...number_of_chars]
14
+ #
15
+ def randpass(chars_no = nil)
16
+
17
+ chars_no = 25 if [nil, 0, 1].include?(chars_no)
18
+
19
+ param = SecureRandom.base64(chars_no)[0...chars_no]
31
20
 
32
21
  rand(param.size / 2).times do
33
22
  param = add_special_characters(param)
@@ -38,6 +27,7 @@ module Randpass
38
27
 
39
28
  alias [] randpass
40
29
 
30
+
41
31
  private
42
32
 
43
33
  def add_special_characters(param)
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fileutils'
4
+
5
+ module Randpass
6
+
7
+ class << self
8
+
9
+ attr_accessor :path
10
+
11
+ # Add password to list
12
+ # @param [String] pass **Required**. Password
13
+ # @param [String] comm Optional. Password comment
14
+ #
15
+ def add(pass, comm = nil)
16
+ reload unless @initialized
17
+ line = comm.nil? ? pass : "#{comm}: #{pass}"
18
+ @temp << line
19
+ end
20
+
21
+ # Clear old password list
22
+ def reload
23
+ @path ||= Dir.pwd
24
+ @temp = Array.new
25
+ @initialized = true
26
+ end
27
+
28
+ # Save list of passwords as txt file
29
+ def finalize
30
+ unless Randpass.nosave?
31
+ temp = @temp.join "\n"
32
+ @path += '/' unless @path.end_with?('/')
33
+ File.write "#{@path}randpass_#{Time.now.to_i}.txt", temp
34
+ end
35
+ @initialized = false
36
+ end
37
+
38
+ # Generate list of passwords, and save as plain txt.
39
+ #
40
+ # @param [Integer] chars_no **Required**. Number of password characters.
41
+ # @param [Hash] opts Optional. Options hash.
42
+ #
43
+ # @option opts [String] comment Add comment (name) with password.
44
+ # @option opts [Integer] list Number of passwords to generate (if no comments)
45
+ #
46
+ def generate_list(chars_no, opts = {list: 10})
47
+ if opts[:comments].nil?
48
+ opts[:list].times do
49
+ add Randpass[chars_no]
50
+ end
51
+ else
52
+ opts[:comments].each do |com|
53
+ add Randpass[chars_no], com
54
+ end
55
+ end
56
+
57
+ Randpass.finalize
58
+ end
59
+
60
+ # Access password list
61
+ def storage
62
+ @temp
63
+ end
64
+
65
+
66
+ private
67
+
68
+ def initialized?; @initialized == true end
69
+
70
+ end
71
+ end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Randpass
4
4
  # gem version
5
- VERSION = '0.1.3'
5
+ VERSION = '0.3.0'
6
6
  end
data/lib/randpass.rb CHANGED
@@ -2,3 +2,43 @@
2
2
 
3
3
  require_relative 'randpass/version'
4
4
  require_relative 'randpass/random'
5
+ require_relative 'randpass/storage'
6
+
7
+ # Generate random password with SecureRandom#base64 and a few
8
+ # special characters.
9
+ #
10
+ module Randpass
11
+
12
+ class << self
13
+
14
+ # Check if password(s) will be copied to clipboard
15
+ def nocopy?
16
+ @copy == false
17
+ end
18
+
19
+ # Disable password(s) copy (affect only executable)
20
+ def nocopy!
21
+ @copy = false
22
+ end
23
+
24
+ # Check if password(s) will be printed to STDOUT
25
+ def noprint?
26
+ @output == false
27
+ end
28
+
29
+ # Do not print password(s) to STDOUT (affect only executable)
30
+ def noprint!
31
+ @output = false
32
+ end
33
+
34
+ # Check if password list will be saved as txt file.
35
+ def nosave?
36
+ @save == false
37
+ end
38
+
39
+ # Do not save password list as txt file.
40
+ def nosave!
41
+ @save = false
42
+ end
43
+ end
44
+ end
data/randpass.gemspec CHANGED
@@ -7,8 +7,8 @@ Gem::Specification.new do |s|
7
7
  s.version = Randpass::VERSION
8
8
  s.summary = 'Ruby random password generator'
9
9
  s.description = <<~DESCRIPTION
10
- Generate random password with SecureRandom#base64. Add random number
11
- of random special characters, and shuffle it.
10
+ Ruby random password generator. Allow single password or list, comments,
11
+ use of clipboard and/or STDOUT.
12
12
  DESCRIPTION
13
13
 
14
14
  s.license = 'MIT'
@@ -29,12 +29,19 @@ Gem::Specification.new do |s|
29
29
  s.files = %w[ lib/randpass.rb
30
30
  lib/randpass/version.rb
31
31
  lib/randpass/random.rb
32
+ lib/randpass/storage.rb
33
+ bin/setup_gem.rb
34
+ bin/output.rb
35
+ bin/setup
32
36
  LICENSE
33
37
  README.md
34
38
  randpass.gemspec]
35
39
 
36
40
  s.required_ruby_version = '>= 2.6', '< 3.2'
37
41
 
42
+ s.add_runtime_dependency 'clipboard', '~> 1.3'
43
+ s.add_runtime_dependency 'optimist', '~> 3.0.1'
44
+
38
45
  s.add_development_dependency 'bundler', '~> 2.3'
39
46
  s.add_development_dependency 'rake', '~> 13.0'
40
47
  end
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: randpass
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - alx3dev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-23 00:00:00.000000000 Z
11
+ date: 2022-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: clipboard
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: optimist
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 3.0.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 3.0.1
13
41
  - !ruby/object:Gem::Dependency
14
42
  name: bundler
15
43
  requirement: !ruby/object:Gem::Requirement
@@ -39,8 +67,8 @@ dependencies:
39
67
  - !ruby/object:Gem::Version
40
68
  version: '13.0'
41
69
  description: |
42
- Generate random password with SecureRandom#base64. Add random number
43
- of random special characters, and shuffle it.
70
+ Ruby random password generator. Allow single password or list, comments,
71
+ use of clipboard and/or STDOUT.
44
72
  email:
45
73
  executables:
46
74
  - randpass
@@ -49,9 +77,13 @@ extra_rdoc_files: []
49
77
  files:
50
78
  - LICENSE
51
79
  - README.md
80
+ - bin/output.rb
52
81
  - bin/randpass
82
+ - bin/setup
83
+ - bin/setup_gem.rb
53
84
  - lib/randpass.rb
54
85
  - lib/randpass/random.rb
86
+ - lib/randpass/storage.rb
55
87
  - lib/randpass/version.rb
56
88
  - randpass.gemspec
57
89
  homepage: https://github.com/alx3dev/randpass
@@ -62,7 +94,7 @@ metadata:
62
94
  source_code_uri: https://github.com/alx3dev/randpass
63
95
  bug_tracker_uri: https://github.com/alx3dev/randpass/issues
64
96
  changelog_uri: https://github.com/alx3dev/randpass/changelog.md
65
- documentation_uri: https://rubydoc.info/gems/randpass/0.1.3
97
+ documentation_uri: https://rubydoc.info/gems/randpass/0.3.0
66
98
  rubygems_mfa_required: 'true'
67
99
  post_install_message:
68
100
  rdoc_options: []
@@ -82,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
114
  - !ruby/object:Gem::Version
83
115
  version: '0'
84
116
  requirements: []
85
- rubygems_version: 3.3.6
117
+ rubygems_version: 3.3.26
86
118
  signing_key:
87
119
  specification_version: 4
88
120
  summary: Ruby random password generator