randpass 0.1.3 → 0.2.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: cbb8602dd6f07d03f70be18df83861597616e0f3c71608cafe2f4b5453857879
4
+ data.tar.gz: 93948d8bebdf6d8fbcbb7e8c36ac196e2b4bc210cdade6016d624f90f45b024a
5
5
  SHA512:
6
- metadata.gz: 938d2f69909daca1f9488c8ea23837a29caa25a995039a7b526e6a1d9c5ce66035fde1a62135544f52950028e55c952cc8b8aa077f8ed4b68d9521a5c914ba15
7
- data.tar.gz: b7ec2a62282de7423cc955b4073fd0acf9a43dc7494721a6bc7b111a753557de8b5bac6b9b90e71a947f633f5285bc579549329a139b3e8b554d6040d568dc5e
6
+ metadata.gz: b08fa98c352f7de784e226ff7e3fbc6dff57ee6937113300386767e6557d407547591b971fb2492d58f3c6f68197c7acfc8a1a031dce99ff8d8e8c9469945d65
7
+ data.tar.gz: f8b505b3194cab5340894203b38d0e7dd379445c714bb9049cb56d732bb8ba9e477a54a26be1024a841ee4cfb049122dcae9b74062a64d24f6ec9c1475ab6958
data/README.md CHANGED
@@ -1,68 +1,72 @@
1
+ ### UNDER DEVELOPMENT
2
+
1
3
  # Randpass
2
4
 
3
- Create random password with ruby. Use `SecureRandom#base64` + few random special characters.
5
+ V-0.2.0:
6
+ - copy password in clipboard
7
+ - add storage module
8
+
9
+
10
+ Ruby random password generator.
11
+ Generate password and copy in clipboard, or generate list and save as `.txt` file.
12
+ File is saved as plain text, it is up to end-user to decide how to secure it.
13
+
14
+ Password is generated with `SecureRandom#base64` and a few special characters `! # * $ % _ @`
15
+
16
+
17
+ ## Dependencies
18
+
19
+ Randpass use _clipboard-gem_, that require `xsel` or `xclip` on Linux systems.
20
+ Setup script will check and install `xsel` if nothing is found.
21
+
22
+
23
+ ## How to use:
24
+
25
+ Use from terminal:
26
+
27
+ - generate password with 25 characters
28
+ `randpass`
29
+
30
+ - generate password with 30 characters
31
+ `randpass 30`
32
+
33
+ - clear clipboard
34
+ `randpass -c`
35
+
36
+ - generate password list from comments (20 characters long password)
37
+ `randpass -n 20 -l password1 password2 password3`
38
+
39
+ - generate password list with 10 passwords - without comments
40
+ `randpass -r 10`
41
+ `randpass -n 20 -r 10`
42
+
43
+ - add `--noc` and/or `--nop` to diasable copy and stdout, respectively
44
+ `randpass 35 --noc`
45
+ `randpass -r 10 --noc --nop`
46
+
4
47
 
5
48
  ## How to install
6
49
 
7
50
  - install from rubygems
8
51
 
9
52
  ```
10
- gem install randpass
53
+ gem install randpass && randpass -s
11
54
  ```
12
55
  - download from github with ssh
13
56
 
14
57
  ```
15
58
  git clone git@github.com:alx3dev/randpass \
16
- cd randpass && bundle install
59
+ cd randpass && bin/setup
17
60
  ```
18
61
  - download from github with https
19
62
 
20
63
  ```
21
64
  git clone https://www.github.com/alx3dev/randpass \
22
- cd randpass && bundle install
65
+ cd randpass && bin/setup
23
66
  ```
24
67
 
25
- To build your own gem, run `rake bundle`, and install it locally with `gem install pkg/randpass-0.1.3.gem`
26
-
27
- ## How to use:
28
-
29
- - use from terminal
68
+ To build your own gem, run `rake bundle`, and install it locally with `gem install pkg/randpass-0.2.0.gem`
30
69
 
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
70
 
67
71
  Tested on:
68
72
  - ruby `2.7.5`
data/bin/options.rb ADDED
@@ -0,0 +1,71 @@
1
+ require_relative '../lib/randpass/version' unless defined? Randpass::VERSION
2
+
3
+ require 'optimist'
4
+
5
+ BANNER =
6
+ <<~DESC
7
+ randpass
8
+ >> generate 25 chars long password
9
+
10
+ randpass 33
11
+ >> generate 33 chars long password
12
+
13
+ ----------------------------------------
14
+ DESC
15
+
16
+ SETUP_DESCRIPTION =
17
+ <<~DESC
18
+ Install gem dependencies
19
+ xsel or xclip on linux (for clipboard manipulation)
20
+
21
+ ----------------------------------------
22
+ DESC
23
+
24
+ NUMBER_DESCRIPTION =
25
+ <<~DESC
26
+ Number of password characters
27
+ randpass -n 20 -l github rubygems
28
+
29
+ ----------------------------------------
30
+ DESC
31
+
32
+ LIST_DESCRIPTION =
33
+ <<~DESC
34
+ Save list of passwords for given comments
35
+ randpass -l GitHub GitLab StackOverflow
36
+
37
+ ----------------------------------------
38
+ DESC
39
+
40
+ RANDOM_DESCRIPTION =
41
+ <<~DESC
42
+ Save list of passwords (number of passwords to generate)
43
+ randpass -r 5
44
+ randpass -r 5 -n 25
45
+
46
+ ----------------------------------------
47
+ DESC
48
+
49
+ CLIPBOARD_DESCRIPTION =
50
+ <<~DESC
51
+ Empty clipboard
52
+ DESC
53
+
54
+ NO_COPY_DESCRIPTION =
55
+ <<~DESC
56
+ Do not copy password into clipboard
57
+ DESC
58
+
59
+ NO_PRINT_DESCRIPTION =
60
+ <<~DESC
61
+ Do not print output in stdout
62
+ DESC
63
+
64
+
65
+ module Output
66
+
67
+ def self.[](txt)
68
+ STDOUT.puts(txt) unless Randpass.noprint?
69
+ Clipboard.copy(txt) unless Randpass.nocopy?
70
+ end
71
+ end
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 './options'
5
5
 
6
- pass = if ARGV.empty?
7
- Randpass[22]
8
- else
9
- Randpass.randpass ARGV[0].to_i
10
- end
6
+ opts = Optimist.options do
11
7
 
12
- # @todo Add clipboard gem
13
- # Clipboard.copy pass
14
- puts pass
8
+ banner BANNER
9
+
10
+ opt :setup, SETUP_DESCRIPTION, short: 's', long: 'gem-setup'
11
+ opt :num, NUMBER_DESCRIPTION, short: 'n', long: 'no', type: :integer
12
+ opt :list, LIST_DESCRIPTION, short: 'l', long: 'list', type: :strings
13
+ opt :random, RANDOM_DESCRIPTION, short: 'r', long: 'random', type: :integer
14
+ opt :clear, CLIPBOARD_DESCRIPTION, short: 'c', long: 'clear', default: false
15
+ opt :nocopy, NO_COPY_DESCRIPTION, short: 'd', long: 'noc', default: false
16
+ opt :noprint, NO_PRINT_DESCRIPTION, short: 'o', long: 'nop', default: false
17
+
18
+ version "Randpass #{Randpass::VERSION}"
19
+ end
20
+
21
+ if opts[:setup]
22
+
23
+ dir = File.expand_path(__FILE__).gsub('/bin/randpass', '')
24
+ Dir.chdir dir
25
+ system 'bin/setup'
26
+ exit(1)
27
+
28
+ else
29
+ require_relative '../lib/randpass'
30
+ require 'clipboard'
31
+
32
+
33
+ (Clipboard.clear && exit(1)) if opts[:clear]
34
+
35
+ Randpass.nocopy! if opts[:nocopy]
36
+ Randpass.noprint! if opts[:noprint]
37
+
38
+ case
39
+ when opts[:num] && opts[:random]
40
+ Randpass.generate_list opts[:num], list: opts[:random]
41
+ Output[Randpass.storage]
42
+ exit(1)
43
+
44
+ when opts[:random]
45
+ Randpass.generate_list nil, list: opts[:random]
46
+ Output[Randpass.storage]
47
+ exit(1)
48
+
49
+ when opts[:list] && opts[:num]
50
+ Randpass.generate_list opts[:num].to_i, comments: opts[:list]
51
+ Output[Randpass.storage]
52
+ exit(1)
53
+
54
+ when opts[:list]
55
+ Randpass.generate_list nil, comments: opts[:list]
56
+ Output[Randpass.storage]
57
+ exit(1)
58
+
59
+ when ARGV.empty?
60
+ Output[Randpass[20]] && exit(1)
61
+ exit(1)
62
+
63
+ when ARGV.size == 1
64
+ Output[Randpass[ARGV[0].to_i]]
65
+ exit(1)
66
+ end
67
+ end
data/bin/setup ADDED
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ function install_clipboard_dependency() {
6
+
7
+ if !(command -v xsel) >/dev/null 2>&1; then
8
+
9
+ echo "xsel was not found on the system, looking for xclip..."
10
+
11
+ if !(command -v xclip) >/dev/null 2>&1; then
12
+
13
+ echo "xclip also not found, installing xsell..."
14
+ sudo apt install xsel -y
15
+
16
+ else
17
+ echo "but xclip was"
18
+ fi
19
+
20
+ else
21
+ echo "xsel found on the system"
22
+ fi
23
+ }
24
+
25
+ echo -e -n "Type 'w' if you are on Windows system, otherwise press enter: "
26
+ read linux_key
27
+
28
+ if [ "$linux_key" == "w" ] || [ "$linux_key" == "W" ]; then
29
+ bundle install
30
+ else
31
+ install_clipboard_dependency
32
+ bundle install
33
+ fi
@@ -7,6 +7,7 @@ require 'securerandom'
7
7
  # class and instance method, so you can call it or include it.
8
8
  #
9
9
  module Randpass
10
+
10
11
  # Allowed specials. xxx is just a flag for #add_special_chars
11
12
  ARRAY = %w[! # * $ % _ @ xxx].freeze
12
13
 
@@ -14,20 +15,24 @@ module Randpass
14
15
  # Transform to array, shuffle, and join back to string.
15
16
  #
16
17
  # Provide number of characters for password as argument,
17
- # default value is **22**.
18
+ # default value is **25**.
18
19
  #
19
- # @param [Integer] number_of_chars Optional. Number of password characters.
20
+ # @param [Integer] chars_no Optional. Number of password characters.
20
21
  # @return [String]
21
22
  #
22
- def randpass(number_of_chars = nil)
23
- Randpass[number_of_chars]
23
+ def randpass(chars_no = nil)
24
+ Randpass[chars_no]
24
25
  end
25
26
 
27
+
26
28
  class << self
29
+
27
30
  # @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]
31
+ def randpass(chars_no = nil)
32
+
33
+ chars_no = 25 if [nil, 0, 1].include?(chars_no)
34
+
35
+ param = SecureRandom.base64(chars_no)[0...chars_no]
31
36
 
32
37
  rand(param.size / 2).times do
33
38
  param = add_special_characters(param)
@@ -38,6 +43,7 @@ module Randpass
38
43
 
39
44
  alias [] randpass
40
45
 
46
+
41
47
  private
42
48
 
43
49
  def add_special_characters(param)
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tempfile'
4
+ require 'fileutils'
5
+
6
+ module Randpass
7
+
8
+ class << self
9
+
10
+ attr_accessor :path
11
+
12
+ FILE_PATH = File.expand_path(__FILE__).gsub('/lib/randpass/storage.rb', '')
13
+
14
+ # Add password to file
15
+ # @param [String] pass **Required**. Add password to list
16
+ # @param [String] comm Optional. Comment to write with password
17
+ #
18
+ def add(pass, comm = nil)
19
+ storage_init unless @initialized
20
+ @temp << "#{comm}: " unless comm.nil?
21
+ @temp << pass
22
+ @temp << "\n"
23
+ end
24
+
25
+ # Write list as .txt file
26
+ def finalize
27
+ File.write "#{@path}/randpass_#{Time.now.to_i}.txt", @temp
28
+ @initialized = false
29
+ end
30
+
31
+ # Generate list of passwords, and save as plain txt.
32
+ #
33
+ # @param [Integer] chars_no **Required**. Number of password characters.
34
+ # @param [Hash] opts Optional. Options hash.
35
+ #
36
+ # @option opts [String] comment Add comment (name) with password.
37
+ # @option opts [Integer] list Number of passwords to generate (if no comments)
38
+ #
39
+ def generate_list(chars_no, opts = {list: 10})
40
+ storage_init unless initialized?
41
+
42
+ if opts[:comments].nil?
43
+ opts[:list].times do
44
+ add Randpass[chars_no]
45
+ end
46
+ else
47
+ opts[:comments].each do |com|
48
+ add Randpass[chars_no], com
49
+ end
50
+ end
51
+
52
+ Randpass.finalize
53
+ end
54
+
55
+ # Access password list
56
+ def storage
57
+ @temp
58
+ end
59
+
60
+
61
+ private
62
+
63
+ def initialized?; @initialized == true end
64
+
65
+ def storage_init
66
+ @path ||= FILE_PATH
67
+ Dir.chdir @path
68
+ @temp = String.new
69
+ @initialized = true
70
+ end
71
+
72
+ alias :init_storage :storage_init
73
+ end
74
+ end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Randpass
4
4
  # gem version
5
- VERSION = '0.1.3'
5
+ VERSION = '0.2.0'
6
6
  end
data/lib/randpass.rb CHANGED
@@ -2,3 +2,33 @@
2
2
 
3
3
  require_relative 'randpass/version'
4
4
  require_relative 'randpass/random'
5
+ require_relative 'randpass/storage'
6
+
7
+ module Randpass
8
+
9
+ class << self
10
+
11
+ @output = true
12
+ @copy = true
13
+
14
+
15
+ def nocopy?
16
+ @copy == false
17
+ end
18
+
19
+
20
+ def nocopy!
21
+ @copy = false
22
+ end
23
+
24
+
25
+ def noprint?
26
+ @output == false
27
+ end
28
+
29
+
30
+ def noprint!
31
+ @output = false
32
+ end
33
+ end
34
+ end
data/randpass.gemspec CHANGED
@@ -7,8 +7,9 @@ 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
+ Generate single password or list, with or without comments,
11
+ and copy password in clipboard (or not). Password is generated with
12
+ SecureRandom#base64 + random special characters.
12
13
  DESCRIPTION
13
14
 
14
15
  s.license = 'MIT'
@@ -29,12 +30,18 @@ Gem::Specification.new do |s|
29
30
  s.files = %w[ lib/randpass.rb
30
31
  lib/randpass/version.rb
31
32
  lib/randpass/random.rb
33
+ lib/randpass/storage.rb
34
+ bin/options.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.2.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-15 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,9 @@ 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
+ Generate single password or list, with or without comments,
71
+ and copy password in clipboard (or not). Password is generated with
72
+ SecureRandom#base64 + random special characters.
44
73
  email:
45
74
  executables:
46
75
  - randpass
@@ -49,9 +78,12 @@ extra_rdoc_files: []
49
78
  files:
50
79
  - LICENSE
51
80
  - README.md
81
+ - bin/options.rb
52
82
  - bin/randpass
83
+ - bin/setup
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.2.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.3
86
118
  signing_key:
87
119
  specification_version: 4
88
120
  summary: Ruby random password generator