-
Notifications
You must be signed in to change notification settings - Fork 21
/
thunar-openssl-encrypt-decrypt.sh
executable file
·133 lines (114 loc) · 2.43 KB
/
thunar-openssl-encrypt-decrypt.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
132
133
#!/usr/bin/env bash
#
# Symmetric encryption and decryption using openssl.
#
# * Put this file into your home binary dir: ~/bin/
# * Make it executable: chmod +x
#
#
# Required Software:
# -------------------------
# * openssl
# * zenity
#
#
# Thunar Integration
# ------------------------
#
# Command: ~/bin/thunar-openssl-encrypt-decrypt.sh -f %f
# File Pattern: *
# Appear On: Select everything
#
#
# Usage:
# -------------------------
# thunar-openssl-encrypt-decrypt.sh -f <filename>
#
# required:
# -f input filename
#
# Note:
# -------------------------
#
# Feel free to adjust/improve and commit back to:
# https://github.com/cytopia/thunar-custom-actions
#
usage() {
echo "$0 -f <filename>"
echo
echo " required:"
echo " -f input filename"
echo
}
while getopts ":f:" i; do
case "${i}" in
f)
f=${OPTARG}
;;
*)
echo "Error - unrecognized option $1" 1>&2;
usage
;;
esac
done
shift $((OPTIND-1))
# Check if file is specified
if [ -z "${f}" ]; then
echo "Error - no file specified" 1>&2;
usage
exit 1
fi
# Check if gpg exists
if ! command -v openssl >/dev/null 2>&1 ; then
echo "Error - 'gpg' not found." 1>&2
exit 1
fi
# Check if zenity exists
if ! command -v zenity >/dev/null 2>&1 ; then
echo "Error - 'zenity' not found." 1>&2
exit 1
fi
chooseOptions () {
algorithms=$(openssl enc -ciphers 2>&1 \
| grep '^-' \
| xargs \
| sed -e 's/^-//' -e 's/ -/|/g')
CMD="zenity --forms \
--title=\"Encryption/Decryption with OpenSSL\" \
--separator=\"|\" \
--add-combo=\"Action\" \
--combo-values=\"Encrypt|Decrypt\" \
--add-combo=\"Algorithm\" \
--combo-values=\"${algorithms}\" \
--add-password=\"key\""
eval "${CMD}"
}
IFS="|" read -ra options <<< "$(chooseOptions)"
if [ -z "${options[0]}" ]; then
zenity --error --text="No action to perform."
exit 1
elif [ -z "${options[1]}" ]; then
zenity --error --text="No algorithm."
exit 1
elif [ -z "${options[2]}" ]; then
zenity --error --text="No passphrase."
exit 1
fi
cmd_option=""
extension=".out"
case ${options[0]} in
"Encrypt" ) cmd_option="-e" ; extension=".enc"
;;
"Decrypt" ) cmd_option="-d" ; extension=".dec"
;;
esac
alg="-${options[1]}"
error="$(openssl enc ${cmd_option} "${alg}" -md sha1 -k "${options[2]}" -in "${f}" -out "${f}${extension}" 2>&1)"
errno=$?
if [ "$errno" -gt "0" ]; then
zenity --error --text="${error}\nreturn code: ${errno}"
exit 1
else
zenity --info --text="${options[0]}ed."
exit $?
fi