scripts

Various scripts for UNIX-like systems
git clone https://git.tronto.net/scripts
Download | Log | Files | Refs | README

secret (1239B)


      1 #!/bin/sh
      2 
      3 # Encrypt and decrypt files using a fixed cipher and passphrase.
      4 # The first line of the (unencrypted) file is considered the "password",
      5 # the rest can be anything.
      6 
      7 # Usage: secret command file
      8 # Available commands:
      9 #	clip: copy the secret to clipboard; deleted after 10 seconds
     10 #	edit: edit or add a file
     11 #	show: show the full encrypted file
     12 
     13 # Requires: openssl, xsel (for clip only)
     14 
     15 # ksh completion
     16 # set -A complete_sel_1 clip edit show
     17 # set -A complete_sel_2 box/secret
     18 
     19 opts="aes-256-cbc -iter 1000"    # options for openssl
     20 timeout=10                       # timeout for clip, in ms
     21 editor=${EDITOR:-vi}
     22 
     23 if [ -z "$1" ] || [ -z "$2" ]; then
     24 	echo "usage: secret command file"
     25 else
     26 case "$1" in
     27 	clip)
     28 		openssl $opts -d < "$2" | head -n 1 | xargs printf "%s" | xsel -ib
     29 		sleep $timeout && xsel -db &
     30 		;;
     31 	edit)
     32 		tempfile=$(mktemp)
     33 		if [ -f "$2" ]; then
     34 			openssl $opts -d < "$2" > "$tempfile"
     35 		fi
     36 		$editor "$tempfile"
     37 		read -p "Are you sure? [N/yes] " an
     38 		if [ "$an" = yes ] || [ "$an" = Yes ] || [ "$an" = YES ]; then
     39 			openssl $opts < "$tempfile" > "$2"
     40 		else
     41 			echo "Changes discarded"
     42 		fi
     43 		rm "$tempfile"
     44 		;;
     45 	show)
     46 		openssl $opts -d < "$2"
     47 		;;
     48 	*)
     49 		echo "$1: not a valid command"
     50 		;;
     51 esac
     52 fi