Skip to content

Instantly share code, notes, and snippets.

@mmguero
Created April 4, 2026 21:44
Show Gist options
  • Select an option

  • Save mmguero/4e160c27990af56e1f5b56665d769504 to your computer and use it in GitHub Desktop.

Select an option

Save mmguero/4e160c27990af56e1f5b56665d769504 to your computer and use it in GitHub Desktop.
wrappers for password-based decrypt/encrypt with age
# shred a file if possible, and rm it if not
function shred_file {
TARGET="$1"
if [[ -n $TARGET ]] && [[ -f "$TARGET" ]]; then
type shred >/dev/null 2>&1 && shred -f -u "$TARGET" || rm -f "$TARGET"
fi
[[ -n $TARGET ]] && [[ ! -f "$TARGET" ]] && return 0 || return 1
}
# shred a file with user-provided confirmation
function shred_file_confirm {
TARGET="$1"
RETURN_CODE=1
read -p "Remove "$TARGET" [Y/n]? " CONFIRMATION
CONFIRMATION=${CONFIRMATION:-Y}
if [[ $CONFIRMATION =~ ^[Yy] ]]; then
shred_file "$TARGET"
RETURN_CODE=$?
fi
return $RETURN_CODE
}
function encrypt_age() {
if [[ -n "${1}" ]] && [[ -f "${1}" ]]; then
OUTPUT="${1}".$(date +%s).age
age -e -p -o "${OUTPUT}" "${1}" && \
[[ -f "$OUTPUT" ]] && \
ls -l "$OUTPUT" && \
shred_file_confirm "${1}"
else
echo "No file specified, or invalid/nonexistant file" >&2
fi
}
function decrypt_age() {
if [[ -n "${1}" ]] && [[ -f "${1}" ]]; then
OUTPUT="$(echo "${1}" | rev | cut -c16- | rev)"
age -d -o "${OUTPUT}" "${1}" && \
[[ -f "$OUTPUT" ]] && \
ls -l "$OUTPUT" && \
shred_file_confirm "${1}"
else
echo "No file specified, or invalid/nonexistant file" >&2
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment