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
|
# File 'lib/mkpasswd.rb', line 21
def initialize(args)
self.options = OpenStruct.new
options.hashmethod = 'des'
options.hasher = HASHERS[options.hashmethod]
OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename $0} [options] [PASSWORD [SALT]]"
opts.separator 'Encrypts password using the unix-crypt gem with CLI compatible with mkpasswd utility from whois package.'
opts.separator ''
opts.separator 'Options:'
opts.on('-m', '--method=METHOD', String, 'Set hash algorithm [sha-512 (default), sha-256, md5, des]') do |hasher|
if hasher.to_s == 'help'
puts 'Available methods:'
puts HASHERS.keys
exit
end
options.hashmethod = hasher.to_s
options.hasher = HASHERS[options.hashmethod]
raise 'Invalid hash algorithm for -m/--method' if options.hasher.nil?
end
opts.on('-S', '--salt [SALT]', String, 'Provide hash salt') do |salt|
raise 'Invalid salt for -S/--salt' if salt.nil?
options.salt = salt
end
opts.on('-R', '--rounds [ROUNDS]', Integer, 'Set number of hashing rounds (SHA256/SHA512 only)') do |rounds|
raise 'Invalid hashing rounds for -R/--rounds' if rounds.nil? || rounds.to_i <= 0
options.rounds = rounds
end
opts.on_tail('-h', '--help', 'Show this message') do
puts opts
exit
end
opts.on_tail('-V', '--version', 'Show version') do
puts Mkpasswd::VERSION
exit
end
end.parse!(ARGV)
options.password = ARGV.shift
if options.password
$0 = $0 else
options.password = ask_password
end
options.salt = ARGV.shift unless options.salt
saltsize = options.salt.to_s.bytesize
if saltsize > 0 && ( saltsize < 8 || saltsize > 16 )
raise "Wrong salt length: #{saltsize} bytes when 8 <= n <= 16 expected."
end
end
|