-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt.sh
57 lines (49 loc) · 2.63 KB
/
encrypt.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
#!/bin/bash
# Usage: ./encrypt.sh some.file
encrypt() {
IS_DIR=0
FILENAME="$1"
if [[ $FILENAME ]] ; then # If command argument exists...
if [[ -d "$FILENAME" ]] ; then # If $FILENAME is a directory, create a tar archive...
IS_DIR=1
FILENAME="$(echo "$FILENAME" | sed -e 's|\/||')" # Remove the trailing '/' in the command argument, if it exists.
tar -cf "${FILENAME}.tar" "$FILENAME" # Create the tarball.
FILENAME="${FILENAME}.tar" # Set $FILENAME to the tar archive.
elif [[ ! -f "$FILENAME" ]] ; then # If $FILENAME is not a regular file...
echo "$FILENAME is not a valid file or directory."
return 1 # ...exit immediately.
fi
PASSWD_HINT=''
HINT_EXISTS=0
if [[ -f passwd.hint ]] ; then # If passwd.hint file exists...
printf "passwd.hint file found.\n"
HINT_EXISTS=1
NL=$'\n'
while read line ; do # Prepend password hint to $PASSWD_HINT line-by-line.
PASSWD_HINT=$(echo -ne "${PASSWD_HINT}\n${line}")
done < passwd.hint
else # Otherwise, have the user input a password hint.
printf "passwd.hint file not found.\n"
printf "Enter in a password hint: "
read PASSWD_HINT
fi
openssl aes-256-cbc -a -salt -in "$FILENAME" -out "${FILENAME}.aes-256-cbc" # Run OpenSSL.
echo -e "\n$(printf -- '-%.0s' {1..64})\n$(cat "${FILENAME}.aes-256-cbc")" > "${FILENAME}.aes-256-cbc" # Prepend a separator in the encrypted file.
if [[ $HINT_EXISTS == 1 ]] ; then
echo -e "${PASSWD_HINT}$(cat "${FILENAME}.aes-256-cbc")" > "${FILENAME}.aes-256-cbc" # Now, prepend $PASSWD_HINT.
tail -n +2 "${FILENAME}.aes-256-cbc" > tmp # Remove the first line which is empty.
mv tmp "${FILENAME}.aes-256-cbc"
else
echo -e "${PASSWD_HINT}$(cat "${FILENAME}.aes-256-cbc")" > "${FILENAME}.aes-256-cbc" # Now, prepend $PASSWD_HINT.
fi
# NEEDS IMPLEMENTATION: HASH THE FILENAME; ADD THE HASHED FILENAME TO THE TOP OF THE ENCRYPTED FILE
# MAKE IT SO THAT DECRYPTION IS INDEPENDENT FROM THE FILENAME
if [[ $IS_DIR == 1 ]] ; then # Clean up intermediary files.
rm "${FILENAME}"
fi
echo "Now you can safely remove the original file."
else
echo "usage: encrypt source_file"
fi
}
encrypt "$1"