Skip to content

Instantly share code, notes, and snippets.

@SomajitDey
Last active September 26, 2021 15:50
Show Gist options
  • Select an option

  • Save SomajitDey/6b668126b64b5763e71ba2e5d567121d to your computer and use it in GitHub Desktop.

Select an option

Save SomajitDey/6b668126b64b5763e71ba2e5d567121d to your computer and use it in GitHub Desktop.
Minimal Digital Safe (for dummies). Download the file, then make it executable: chmod +x ./mindisafe
#/usr/bin/env bash
# Brief: Minimal Digital Safe (MinDiSafe)
# Usage: mindisafe <path>
trap 'eval ${reset}' exit
keygen(){
echo -n "Safe: Enter Mother's maiden name (case-insensitive): "
read -r salt || return 1
echo -n "Safe: Enter your passkey now: "
read -r pass || return 1
tput cuu 2 ; tput ed ; echo "Safe: Got the creds :)"
key=$(echo -n "${pass}" | "${argon2}" "mindisafe${salt,,*}" -i -t 12 -m 17 -p 4 -l 32 -r)
}
cmd="${0} ${@}"
path="${@}"
[[ -n "${path}" ]] || read -erp "Safe: Drag-n-drop file here - " path || exit 1
path="${path/#\"/}" ; path="${path/%\"/}" # Quote (") removal, if any
converted="$(wslpath "${path}" 2>/dev/null)"
path="${converted:="${path}"}"
path="${path/#~\//${HOME}\/}" # Tilde expansion
path="${path/#~-\//${OLDPWD}\/}" # Tilde expansion
[[ -f "${path}" ]] || { echo "Error: ${path} doesn't exist"; exit 1;}
argon2="$(which argon2)" ; argon2="${argon2:="${HOME}/.argon2.mindisafe"}"
if ! [[ -x "${argon2}" ]]; then
echo "Safe: argon2 not installed. Need to download it. Check your internet connection and then press any key: "
read -sn1
echo -n "Safe: Wait..."
curl -sSNfL -o "${argon2}" \
"https://github.com/SomajitDey/phc-winner-argon2/releases/download/static-build_linux_x86_64/argon2" \
|| { echo "Error: argon2 binary couldn't be downloaded"; exit 1;}
chmod +x "${argon2}"
echo -e "\rSafe: argon2 obtained. Proceeding."
fi
tmp=$(mktemp -u .mindisafeXXXXXX); reset="${reset:-:} ; rm -f ${tmp}"
if grep -zq "^.*BEGIN PGP MESSAGE.*END PGP MESSAGE.*$" "${path}"; then
echo "Safe: You provided a lockedfile. So let's unlock it."
keygen || exit 1
PS3="Safe: Type 1 or 2 - "$'\n'
echo "Safe: Now, do you want to"
select opt in "unlock the file permanently" "just see the contents here";do
case "${REPLY}" in
1)
cp "${path}" "${tmp}" || exit "${?}"
gpg --batch -q --pinentry-mode loopback --passphrase "${key}" -d "${path}" > "${tmp}" && \
mv "${tmp}" "${path}" && \
echo "Safe: Done. To lock the file again, simply: ${cmd}"
break
;;
2)
tput smcup
echo -e "Safe: Here is your data. No worries, the file has been kept locked. Press anykey when you are ready to exit.\n\n"
gpg --batch -q --pinentry-mode loopback --passphrase "${key}" -a -o - -d "${path}"
read -sn1
tput rmcup
break
;;
esac
done
else
echo "Safe: You provided an unlocked file. So let's lock it."
keygen || exit 1
gpg --batch -q --pinentry-mode loopback --passphrase "${key}" -a -o "${tmp}" -c "${path}" && \
mv "${tmp}" "${path}" && \
echo "Safe: File locked. To unlock the file, simply: ${cmd}"
echo -n "Safe: Do you need a download link for the locked file (internet connectivity required)? (y/n): "
read -n1; echo
case "${REPLY,,*}" in
y) echo "Safe: Uploading..." ; echo "Safe: $(curl -sSfN -T "${path}" https://transfer.sh)";;
n) tput cuu1; tput ed;;
*) echo "Safe: You didn't type y so assuming n. Ok bye then";;
esac
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment