-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypt.sh
executable file
·70 lines (58 loc) · 1.19 KB
/
crypt.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
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/sh
#
# Encrypt/decrypt regular file with sensitive information (using a password).
#
usage()
{
if [ $1 -eq 1 ]; then
>&2 echo "Try '$(basename $0) --help' for more information"
exit 1
else
cat << EOF
Usage: $(basename $0) [option]
Use AES 256 (Advanced Encryption Standard) cipher
for symmetric file encrypt/decrypt.
[option]
-f, --file <name> specify file (regular or openssl)
-h, --help show this help and exit
EOF
exit 0
fi
}
case "$1" in
'-f'|'--file')
if [ ! -f "$2" ] || [ -z "$2" ]; then
>&2 echo "\$2 incorrect"
usage 1
fi
if [ "$USER" != "$(stat -c %U $2)" ]; then
>&2 echo "'$USER' you have no perms"
exit 1
fi
randf=".$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 8)"
while true; do
if file "$2" | grep -E "openssl|salted"; then
# decrypt file
if ! openssl enc -d -aes256 -pbkdf2 -in "$2" 2>/dev/null; then
>&2 echo "Something went wrong, try again"
exit 1
fi
else
# encrypt new file
if ! openssl enc -e -aes256 -pbkdf2 -in "$2" -out "$randf"; then
continue
fi
shred -uzn 8 "$2" && echo "shred $2"
chmod 600 "$randf"
break
fi
break
done
;;
'-h'|'--help')
usage 0
;;
*)
usage 1
;;
esac