-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsimple_passwd.rb
More file actions
executable file
·106 lines (93 loc) · 2.88 KB
/
simple_passwd.rb
File metadata and controls
executable file
·106 lines (93 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env ruby
########################################################################
# simple_password.rb: Simple Password Generator in Ruby
#
# Description:
# This script generates a random password. It can optionally exclude
# special symbols from the password. If symbols are enabled, at least
# one symbol is guaranteed to be included in the password.
#
# Author: id774 (More info: http://id774.net)
# Source Code: https://github.com/id774/scripts
# License: The GPL version 3, or LGPL version 3 (Dual License).
# Contact: idnanashi@gmail.com
#
# Usage:
# simple_password.rb [options] length
#
# Options:
# -s, --no-symbols Do not include symbols in the password
#
# Requirements:
# - Ruby Version: 2.0 or later
#
# Version History:
# v1.5 2025-07-03
# Ensure at least one symbol is included when use_symbols is enabled.
# v1.4 2025-07-01
# Standardized termination behavior for consistent script execution.
# Refactored into main function for consistency and maintainability.
# v1.3 2025-06-23
# Unified usage output to display full script header and support common help/version options.
# v1.2 2025-04-13
# Unify log level formatting using [INFO], [WARN], and [ERROR] tags.
# v1.1 2023-12-06
# Added option to exclude special symbols and improved argument handling.
# v1.0 2013-01-07
# Initial release.
#
########################################################################
require 'optparse'
def usage
script = File.expand_path(__FILE__)
in_header = false
File.foreach(script) do |line|
if line.strip.start_with?('#' * 10)
in_header = !in_header
next
end
puts line.sub(/^# ?/, '') if in_header && line.strip.start_with?('#')
end
exit 0
end
def generate_passwd(length, use_symbols = true)
if length <= 0
warn "[ERROR] Length must be greater than zero."
exit 1
end
base_chars = [*'0'..'9', *'a'..'z', *'A'..'Z']
symbol_chars = ['_', '-', '!', '#', '&']
if use_symbols
if length == 1
puts symbol_chars.sample
return
end
chars = base_chars + symbol_chars
password = [symbol_chars.sample] + Array.new(length - 1) { chars.sample }
puts password.shuffle.join
else
puts Array.new(length) { base_chars.sample }.join
end
end
def main
if ARGV.empty? || ['-h', '--help', '-v', '--version'].include?(ARGV[0])
usage
end
options = { use_symbols: true }
option_parser = OptionParser.new do |opts|
opts.banner = "Usage: simple_password.rb [options] length"
opts.on("-s", "--no-symbols", "Do not include symbols in the password") do
options[:use_symbols] = false
end
end
option_parser.parse!
if ARGV.length != 1 || ARGV[0] !~ /\A\d+\z/
puts "[ERROR] Length must be a number."
puts option_parser
return 1
end
length = ARGV.shift.to_i
generate_passwd(length, options[:use_symbols])
return 0
end
exit(main) if __FILE__ == $0