-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencFile
More file actions
executable file
·76 lines (63 loc) · 1.73 KB
/
encFile
File metadata and controls
executable file
·76 lines (63 loc) · 1.73 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
#!/usr/bin/env bash
#
# /usr/local/xbin/encFile
#
# Description:
# Encrypt or Decrypt files with openssl
#
#
# Since openssl 1.1.1b:
# *** WARNING : deprecated key derivation used.
# Using -iter or -pbkdf2 would be better.
#
# Supress it, for now (2> /dev/null)
#
ERR=0;
_base=$(basename ${0});
[[ ! $(type openssl 2> /dev/null) ]] && echo "${_base}: Please install: openssl" && exit 1;
while getopts ":c:h" opt; do
case ${opt} in
c) _c=${OPTARG}; ;;
h)
cat << HELP
Usage: ${_base} [-h] [-c cipher] <file>
Options:
-c CIPHER Cipher can be one of: "aes" (aes256), or "bf" (default).
-h Show this help.
HELP
;;
*)
echo -e "${_base}: -${OPTARG}: Not a valid option.\n" >&2;
${_base} -h;
exit 1;
;;
esac
done
shift "$((OPTIND -1))";
# bf or aes
[[ ${_c} =~ ^bf$|^aes$|^aes256$ ]] && _c="${_c}" || _c='bf';
[[ ${_c} =~ ^aes$ ]] && _c='aes256';
# Set _enc
_file="${1}";
[[ ${_file##*.} == 'bf' ]] && _enc='bf';
[[ ${_file##*.} == 'aes' ]] && _enc='aes256';
if [[ ${#} == 1 ]]; then
# Check file
if [[ -d "${_file}" || ! -f "${_file}" ]]; then
echo -e "${_base}: \"${_file}\" is not a valid or existing file.\n";
${_base} -h && exit 1;
# Encrypt
elif [[ -n ${_c} && -z ${_enc} ]]; then
echo ":: Encrypting: ${_file}";
openssl enc -${_c} -A -in "${_file}" > "${_file}.${_c//256/}" 2> /dev/null;
ERR=${?};
# Decrypt
else
echo -e ":: Decrypting: ${_file}";
openssl enc -${_enc} -A -d -in "${_file}" > "${_file%.*}" 2> /dev/null;
ERR=${?};
fi
# Remove ${_file}
[[ ${ERR} == 0 ]] && echo ":: Removing: $(rm -v "${_file}")";
fi
exit ${ERR};