aboutsummaryrefslogtreecommitdiff
path: root/utils/encrypt
blob: d1cb9385321f03c0ff977105374b3c031edb3d08 (plain)
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
# vim:ft=sh:noexpandtab
# Allows work with encrypted files using their temporally copy.

if [ -n "$KEY_FILE" ] && [ -f "files/keys/$(hostname)" ]; then
	SECRET_KEY="$(openssl aes-192-cbc -d -a -kfile "$KEY_FILE" -in files/keys/"$(hostname)")"
else
	SECRET_KEY="$(gpg2 --decrypt files/keys/primary.gpg)"
fi

# Decrypt to temporally file in /tmp
# First argument should be the path to file to be decrypted and second argument
# output file.
fdecrypt() {
	PASS_ENC="$SECRET_KEY" openssl aes-192-cbc -d -a -pass env:PASS_ENC -in "$1" -out "$2"
}

# Encrypt given file to target path
# First argument have to be a path to file to be encrypted and second argument
# output file.
fencprypt() {
	PASS_ENC="$SECRET_KEY" openssl aes-192-cbc -e -a -pass env:PASS_ENC -in "$1" -out "$2"
}

# Decrypt file to temporally one
# First argument have to be path to file to be extracted.
tfdecrypt() {
	local TEMPF="$(mktemp /tmp/multiconfig_XXXXXXXX)"
	fdecrypt "$1" "$TEMPF"
	echo "$TEMPF"
}

# Cat decrypted file
# First argument have to be a path to file to be catted.
catdecrypt() {
	PASS_ENC="$SECRET_KEY" openssl aes-192-cbc -d -a -pass env:PASS_ENC -in "$1"
}

# Encrypt stdin to file
# First argument have to be a path to output file.
outencrypt() {
	PASS_ENC="$SECRET_KEY" openssl aes-192-cbc -e -a -pass env:PASS_ENC -out "$2"
}