-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt
More file actions
executable file
·128 lines (119 loc) · 3.5 KB
/
Copy pathencrypt
File metadata and controls
executable file
·128 lines (119 loc) · 3.5 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#! /bin/bash
#Print Usage Information
usage() {
printf "Usage: [PROGRAM_NAME] [OPTION] [FILE/DIRECTORY]\n"
printf "Options: [ -f | --file ] [ -df | --decrypt-file] [ -d | --directory ] [ -dd | --decrypt-directory]\n"
}
#Check The Number Of Arguemtns
check_args() {
if [ "$#" -ne 2 ]; then
usage
exit_func 1
elif [ "$#" -eq 1 ]; then
usage
exit_func 1
elif [ "$#" -eq 2 ]; then
return 0
fi
}
#Check That Each Of The main Commands Used Executed Correctly
execution_check() {
if [ "$1" -eq 0 ]; then
return 0
elif [ "$1" -eq 1 ]; then
echo "Error Executiong 'shred' Command."
exit_func 3
elif [ "$1" -eq 2 ]; then
echo "Error Executing 'gpg' Command."
exit_func 3
fi
}
#Print Exit Information
exit_func () {
if [ "$1" -eq 1 ]; then
printf "Exit 1 --> Wrong Number of Arguments.\n"
usage
exit 1
elif [ "$1" -eq 2 ]; then
printf "File or Directory Arguments Were Incorrect.\n"
usage
exit 2
elif [ "$1" -eq 3 ]; then
usage
exit 3
elif [ "$1" -eq 0 ]; then
printf "Exit 0 --> Success!\n"
exit 0
fi
}
#Main Program Loop
if [ "$(check_args)" ]; then
#While There Are Arguments
while [ "$1" != "" ]; do
#Switch On The Arguments
case $1 in
#Encrypt A Single File
-f | --file ) if [ -f $2 ]; then
#Use GPG To Encrypt, But Because GPG Leaves Behind An Unencrypted Version
#Use Shred Command To Get Rid Of Unencrypted Copy Left Behind
gpg -c --cipher-algo AES256 $2
execution_check "$?"
shred -u -n $(shuf -i 3-10 -n 1) $2
execution_check "$?"
exit_func 0
else
#If Input Is Not A File, Print Error
printf "%s is not a file.\n" $2
exit_func 2
fi
;;
#Decrypt A Single File
-df | --decrypt-file ) if [ -f $2 ]; then
#Decrypt The Encrypted File and Get Rid of Encrypted File Left Behind
gpg $2
execution_check "$?"
shred -u -n $(shuf -i 3-10 -n 1) $2
execution_check "$?"
exit_func 0
else
#If Input Is Not File, Print Error
printf "%s is not a file.\n" $2
exit_func 2
fi
;;
#Encrypt A Directory
-d | --directory ) if [ -d $2 ]; then
#Tar The Directory, Then Use GPG To Encrypt Tar File and Shred to Remove Leftover File
name=${2%/}
tar -cvf "$name".tar "$2"
gpg -c --cipher-algo AES256 "$name.tar"
execution_check "$?"
shred -u -n $(shuf -i 3-10 -n 1) "$name.tar"
execution_check "$?"
rm -rf $2
exit_func 0
else
#If Input Is Not A Directory, Print Error
printf "%s is not a directory.\n" $2
exit_func 2
fi
;;
#Decrypt A Directory
-dd | --decrypt-directory ) if [ -f $2 ]; then
#Decrypt Tar File And Extract Tar File
#Delete Any Encrypted Copies Left Behind
gpg -o- $2 | tar xvf -
execution_check "$?"
shred -u -n $(shuf -i 3-10 -n 1) $2
execution_check "$?"
exit_func 0
else
#If Input Is Not A Directory, Print Error
printf "%s is not a directory.\n" $2
exit_func 2
fi
;;
* ) exit_func 1
esac
done
fi