#!/bin/sh
# enc: encrypt a password from stdin with a passphrase
# add at least one readable line to the password so you can confirm that the
# output is correct when you're decrypting later, as openssl doesn't give
# errors about wrong passphrases and spits out garbled output instead
set -e
[ "$#" -ne 1 ] && {
  echo "usage: ${0##*/} passname" 1>&2
  exit 1
}
command -p -- mkdir -p -m 0700 -- "${HOME}/.passwords/"
passfile="${HOME}/.passwords/${1}.cha20"
if [ -r "${passfile}" ]; then
  res=''
  while [ "${res}" != 'yes' ]; do
    printf 'file %s already exists, overwrite it? (yes,no)[no] ' "${passfile}"
    read -r res
    if [ "${res}" = '' ] || [ "${res}" = 'no' ]; then
      exit
    fi
  done
fi
openssl enc -e -chacha20 -pbkdf2 -iter 10000 -out "${passfile}"
