Created
April 4, 2026 21:44
-
-
Save mmguero/4e160c27990af56e1f5b56665d769504 to your computer and use it in GitHub Desktop.
wrappers for password-based decrypt/encrypt with age
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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