-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathdotfiles-encrypt
executable file
·55 lines (47 loc) · 1001 Bytes
/
dotfiles-encrypt
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
#!/bin/sh
set -e
PASSWDFILE=~/.dotfiles/.passwd
if ! [ -f $PASSWDFILE ]; then
printf "Missing password file $PASSWDFILE.\nPassword: "
read passwd
tempfile=$(mktemp --tmpdir dotfiles-encrypt.XXX)
trap 'rm -f "$tempfile" >/dev/null 2>&1' 0
trap 'exit 2' 1 2 3 15
echo $passwd > $tempfile
PASSWDFILE="$tempfile"
fi
basename=$(basename $0)
if [ "$basename" = "dotfiles-decrypt-verify" ]; then
# Test if decryption works properly.
dotfiles-decrypt "U2FsdGVkX1+T+vo4rP6Ah5NQz/DJjBhsIYiHKb6S2+k="
exit
fi
md=sha256
if [ "$1" = '-md' ]; then
md="$2"
shift 2
fi
if [ x$# = x0 ]; then
input=
if [ -t 0 ]; then
printf "Input: "
read input
fi
else
input="$@"
fi
cmd="openssl enc -md $md -aes-256-cbc -pbkdf2 -a -A -salt -pass file:$PASSWDFILE"
case $basename in
dotfiles-encrypt)
;;
dotfiles-decrypt)
cmd="$cmd -d"
;;
*) echo "Unhandled basename: $basename." >&2; exit 64
;;
esac
if [ -n "$input" ]; then
echo "$input" | $cmd
else
$cmd
fi