enc (1143B)
1 #!/bin/sh 2 set -eu 3 4 readonly Command='openssl enc -ChaCha20 -pbkdf2' 5 6 base64= 7 stdout=false 8 mode=encrypt 9 force=false 10 11 while getopts 'acdef' opt; do 12 case $opt in 13 (a) base64=-a;; 14 (c) stdout=true;; 15 (d) mode=decrypt;; 16 (e) mode=encrypt;; 17 (f) force=true;; 18 (?) exit 1;; 19 esac 20 done 21 shift $((OPTIND - 1)) 22 23 confirm() { 24 $force && return 0 25 while :; do 26 printf '%s: overwrite %s? [y/N] ' "$0" "$1" >&2 27 read -r confirm 28 case "$confirm" in 29 (Y*|y*) return 0;; 30 (N*|n*|'') return 1;; 31 esac 32 done 33 } 34 35 encrypt() { 36 if test -z "${1:-}"; then 37 $Command -e $base64 38 elif $stdout; then 39 $Command -e $base64 -in "$1" 40 else 41 input=$1 42 output="${1}.enc" 43 if test -e "$output" && ! confirm "$output"; then 44 return 45 fi 46 $Command -e $base64 -in "$input" -out "$output" 47 fi 48 } 49 50 decrypt() { 51 if test -z "${1:-}"; then 52 $Command -d $base64 53 elif $stdout || [ "${1%.enc}" = "$1" ]; then 54 $Command -d $base64 -in "$1" 55 else 56 input=$1 57 output=${1%.enc} 58 if test -e "$output" && ! confirm "$output"; then 59 return 60 fi 61 $Command -d $base64 -in "$input" -out "$output" 62 fi 63 } 64 65 for input; do 66 $mode "$input" 67 done 68 if [ $# -eq 0 ]; then 69 $mode 70 fi