Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 56 lines (50 sloc) 1.21 KB
#!/bin/bash
function check_exist() {
if [ -e "$1" ]
then
echo "File '$1' already exists"
prompt_remove "$1" || return 1
fi
}
function prompt_remove() {
read -p "Remove file: $1?" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
rm -rf "$1" && echo "File $1 removed" && return 0
fi
return 1
}
function encrypt_dir() {
local base=$(basename "$1")
local enc_name="$base.enc"
check_exist "$enc_name" || return 1
echo "Creating and encrypting '$1' -> '$enc_name'..."
(tar -cf - "$1" | gpg --cipher-algo AES256 -co "$enc_name") || (echo "Failed to create an encrypted file" && return 1)
echo "Successfully encrypted: $enc_name"
prompt_remove "$1" || return 0 # file removal is optional
}
function decrypt_dir() {
local base=$(basename "$1")
local dest="${base%.*}"
check_exist "$dest" || return 1
echo "Decrypting and extracting '$1' -> '$dest'..."
(gpg -d "$1" | tar -x) || (echo "Failed to decrypt '$1'" && return 1)
}
case "$1" in
enc)
encrypt_dir "$2" || exit 1
;;
dec)
decrypt_dir "$2" || exit 1
;;
*)
echo "Usage: vault COMMAND FILE"
echo
echo "Encrypts/Decrypts a file/directory with AES256"
echo
echo "Commands:"
echo "enc — encrypt file"
echo "dec — decrypt file"
;;
esac
You can’t perform that action at this time.