forked from ecin/hashapass.rb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhashapass.rb
executable file
·202 lines (151 loc) · 4.15 KB
/
hashapass.rb
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env ruby
# Tired of remembering passwords? Hash them! This is a terminal app to
# simplify generation and copying to clipboard. Passwords are compatible
# with http://www.hashapass.com
#
# Examples::
# If you wish to create a password for your gmail account...
#
# hashapass gmail
#
# ... and you'll be asked for your master password.
#
# Usage::
# hashapass [-cphv] something_simple
#
# Options::
# * -c, --confirm Confirm password
# * -p, --print Print generated password
# * -h, --help Displays help message
# * -v, --version Display the version
#
# Author:: [email protected]
# Copyright:: (c) 2009 [email protected]. Licensed under the MIT License:
# http://www.opensource.org/licenses/mit-license.php
# Thanks:: todd werth (http://blog.infinitered.com/entries/show/5) for the skeleton of this command-line app
require 'openssl'
require 'base64'
require 'rdoc/usage'
require 'optparse'
require 'ostruct'
class App
attr_reader :options
def initialize(arguments, stdin)
@arguments = arguments
@stdin = stdin
@options = OpenStruct.new
end
# Parse options, check arguments, then process the command
def run
if parsed_options? && arguments_valid?
process_arguments
process_command
else
output_usage
end
end
protected
# True if required arguments were provided
def arguments_valid?
true if @arguments.length == argv_length
end
# Setup the arguments
def process_arguments
@parameter = @arguments.first.chomp
end
def output_help
output_version
RDoc::usage('usage') #exits app
end
def output_usage
RDoc::usage('usage') # gets usage from comments above
end
def output_version
puts "#{File.basename(__FILE__)} #{version}"
end
def process_command
end
end
module Hashapass
@@version = '0.2'
@@argv_length = 1
attr_accessor :terminal_settings, :parameter, :password
def version
@@version
end
def argv_length
@@argv_length
end
def parsed_options?
# Specify options
opts = OptionParser.new
opts.on('-v', '--version') { output_version ; exit 0 }
opts.on('-h', '--help') { output_help; exit 0 }
opts.on('-c', '--confirm') { @options.confirm = true }
opts.on('-p', '--print') { @options.print = true }
opts.parse!(@arguments) rescue return false
true
end
def process_command
# Get current terminal settings to restore later on
get_terminal_defaults
# Ask for hashing password
print 'Password: '
# Turn off echo in the current terminal to accept a password
turn_off_echo
@password = STDIN.gets.chomp!
# If user wants password confirmation...
if @options.confirm
# Let's turn on echo again...
turn_on_echo
puts
print 'Password confirmation: '
# ... and off again.
turn_off_echo
# Get password confirmation...
password_confirmation = STDIN.gets.chomp!
# And compare it with previous password input
unless @password == password_confirmation
puts
puts 'Passwords don\'t match, exiting...'
turn_on_echo
exit 0
end
end
# Print a newline to be pretty
puts
# Restore default terminal settings
turn_on_echo
# Generate password...
hash = hash_password(@parameter, @password)
# ... and copy it to the clipboard
copy_to_clipboard(hash)
# If user wants the password to be displayed...
if @options.print
puts "Hashed password: #{hash}"
end
# Let the user know what just happened
puts 'Hashed password copied to clipboard!'
exit 0
end
private
def hash_password(parameter, password)
Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::SHA1.new, password, parameter))[0..7]
end
def get_terminal_defaults
@terminal_settings = `stty -g`
end
def copy_to_clipboard(text)
Kernel.system("printf #{text} | pbcopy")
end
def turn_on_echo
Kernel.system("stty #{terminal_settings}")
end
def turn_off_echo
Kernel.system('stty -echo')
end
end
# Create and run the application
app = App.new(ARGV, STDIN)
app.extend Hashapass
app.run