-
Notifications
You must be signed in to change notification settings - Fork 0
/
decryptFile
executable file
·82 lines (69 loc) · 1.63 KB
/
decryptFile
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
#!/bin/bash
# See also encryptFile
# SETUP --------------------------------------------------------------
ENCKEY=''
KDF='pbkdf2'
ITER='5555555' # iterations for key derivation # ~6 seconds @ Intel(R) Xeon(R) CPU E3-1230 v3 @ 3.30GHz
CIPHER='aes-256-cbc'
DIGEST='sha256'
# --------------------------------------------------------------------
set -ueo pipefail
# pv
BLOCKSIZE=16
if [ $# -lt 1 ]; then
APP=${0##*/}
echo "Decrypt a file."
echo
echo "KDF: $KDF"
echo "Iterations: $ITER"
echo "Cipher: $CIPHER"
echo "Digest: $DIGEST"
echo
echo "Usage: $APP <input> [<output>]"
echo
echo "If <output> is empty STDOUT will be used."
echo
exit 1
fi >&2
if [ ! -e "$1" ]; then # -f does not catch FIFOs
echo -e "Error: File '$1' not found.\n" >&2
exit 1
fi
checkBinarys() {
#http://stackoverflow.com/a/677212/568737
BINS=("$@")
for BIN in "${BINS[@]}"; do
hash "$BIN" 2>/dev/null || {
echo -e "Error: Binary '$BIN' is missing.\n" >&2
exit 1
}
done
}
# Check if all needed binarys are present
checkBinarys "openssl" "pv" "stat"
if [ -z "${ENCKEY:-}" ]; then
read -r -s -p "Enter password: " ENCKEY
echo
[ -z "$ENCKEY" ] && echo && exit 1
fi
CMD=("openssl" "$CIPHER" "-d" "-salt" "-$KDF" "-iter" "$ITER" "-md" "$DIGEST" "-k" "$ENCKEY" "-in" "$1")
if [ -z "${2:-}" ]; then
# STDOUT
echo "Decrypting: $1" >&2
echo >&2
if ! "${CMD[@]}"; then
# Decryption failed.
echo
echo
exit 1
fi >&2
else
# OUTPUT to FILE
echo "Decrypting: $1 --> $2"
if ! "${CMD[@]}" | pv -bper -s $(( ($(stat -c%s "$1") + BLOCKSIZE) / BLOCKSIZE * BLOCKSIZE + BLOCKSIZE)) > "$2"; then
# Decryption failed.
echo
exit 1
fi
echo
fi