Skip to content

Instantly share code, notes, and snippets.

@dmrub
Last active September 18, 2018 08:56
Show Gist options
  • Save dmrub/1481f1f63b875e97a5e3e054ee48d720 to your computer and use it in GitHub Desktop.
Save dmrub/1481f1f63b875e97a5e3e054ee48d720 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
if ! [[ -x "$(command -v curl 2> /dev/null)" ]]; then
echo >&2 "No curl command"
exit 1
fi
if ! [[ -x "$(command -v openssl 2> /dev/null)" ]]; then
echo >&2 "No openssl command"
exit 1
fi
randpw() { tr -dc _A-Z-a-z-0-9 < /dev/urandom | head -c16; }
transfer() { curl --progress-bar --upload-file "$1" "https://transfer.sh/$2" | tee /dev/null; }
case "$1" in
-e|--encrypt)
shift
if [[ -z "$1" ]]; then
echo >&2 "File name is missing"
exit 1
fi
if [[ -z "$PASSWORD" ]]; then
PASSWORD=$(randpw)
echo "Use random password: $PASSWORD"
else
echo "Use password from PASSWORD environment variable"
fi
transfer <(openssl enc -aes-256-cbc -md sha256 -kfile <(echo -n "$PASSWORD") -in "$1") "$(basename "$1")"
echo
;;
-d|--decrypt)
shift
if [[ -z "$PASSWORD" ]]; then
echo -n Password:
read -rs PASSWORD
echo
if [[ -z "$PASSWORD" ]]; then
echo >&2 "Password is empty"
exit 1
fi
fi
DEST_FN=$(basename "$1")
if [[ -e "$DEST_FN" ]]; then
DEST_FN=$(mktemp -p "$PWD" -t "${DEST_FN}.XXXXXX")
fi
curl -s "$1" | openssl enc -d -aes-256-cbc -md sha256 -kfile <(echo -n "$PASSWORD") -out "$DEST_FN"
echo "Decrypted file: $DEST_FN"
;;
-h|--help|*)
echo "Usage:"
echo " -e|--encrypt file-name Encrypt and transfer"
echo " -d|--decrypt url Download and decrypt (password must be in PASSWORD environment variable)"
echo " -h|--help Print this"
exit 0
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment