forked from ecin/hashapass.rb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hashapass.sh
executable file
·131 lines (118 loc) · 2.7 KB
/
hashapass.sh
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
#!/bin/bash
# Author: copypastel.com
# Copyright 2010
# Contact:
# - daicoden@copypastel.com
# - ecin@copypastel.com
VERSION="0.3"
function output_version {
printf "hashapass %s \n" $VERSION
}
function output_help {
echo "Tired of remembering passwords? Hash them! This is a terminal app to"
echo "simplify generation and copying to clipboard. Passwords are compatible"
echo "with http://www.hashapass.com"
echo ""
echo "Example:"
echo "If you wish to create a password for your gmail account..."
echo ""
echo "hashapass gmail"
echo
echo "... and you'll be asked for your master password."
echo
echo "Usage:"
echo "hashapass [-nphv] [-l length] something_simple"
echo
echo "Options:"
echo "-n Confirm password."
echo "-l Length of password (12 characters by default)."
echo "-p Print generated password."
echo "-h Displays help message."
echo "-v Display version."
echo ""
echo "Licensed under the MIT license:"
echo "http://www.opensource.org/licenses/mit-license.php"
}
function echo_off {
stty -echo
}
function echo_on {
stty echo
}
function assert_matching {
if [ $1 != $2 ]
then
printf "Passwords do not match. Exiting...\n"
exit
fi
}
function hidden_read {
printf $2
echo_off
read tmp
echo_on
printf "\n"
eval "$1=$tmp"
}
function hash_password() {
# $1 is the return result
# $2 is the paramater
# $3 is the password
# $4 is the password length
eval "$1=`printf $2 | openssl dgst -sha1 -hmac $3 -binary | openssl enc -base64 | head -c $4`"
}
nFlag= # New Flag
pFlag= # Print Flag
length=12
while getopts 'vhnpl:' OPTION
do
case $OPTION in
v) # Version
output_version
exit
;;
h) # Help
output_help
exit
;;
n) # New Password
nFlag=1
;;
p) # Print Password
pFlag=1
;;
l) # Password length
length=$OPTARG
;;
esac
done
# Now all options are removed
shift $(($OPTIND - 1))
paramater=$1
hidden_read password "Password:"
if [ "$nFlag" ]
then
hidden_read password_conf "Confirmation:"
assert_matching $password $password_conf
fi
hash_password result $paramater $password $length
if [ "$pFlag" ]
then
printf "%s\n" $result
else
echo_off
if which pbcopy &> /dev/null
then
printf $result | pbcopy
elif which xsel &> /dev/null
then
printf $result | xsel -ib
else
echo_on
echo "xsel or pbcopy not found, cannot copy to clipboard."
echo "Use -p to print out the password, or modify the script."
exit
fi
echo_on
printf "Result copied to clipboard.\n"
fi