Add encryption support.

This commit is contained in:
bʰedoh₂ swé 2025-01-12 18:31:18 +05:00
parent a5b9e4612f
commit 35d0b4753f
2 changed files with 45 additions and 3 deletions

View File

@ -29,5 +29,9 @@ Available commands:
* q: Exit. * q: Exit.
* e: Toggle encryption
* E: Set encryption password. (Can also be set via "DOBROHO_VECHORA_ENCRYPTION_PASSWORD" environment variable.)
Client prints "?" on unknown commands. Client prints "?" on unknown commands.

View File

@ -62,10 +62,34 @@ case "${mode}" in
exit exit
esac esac
encrypt() {
if [ "${enc}" -eq 1 ] | [ -z "${DOBROHO_VECHORA_ENCRYPTION_PASSWORD}" ]; then
cat
return 0
fi
echo "ENCRYPTED"'!'"<$(openssl enc -pbkdf2 -a -aes256 -pass env:DOBROHO_VECHORA_ENCRYPTION_PASSWORD)>"
}
decrypt() {
if [ "${enc}" -eq 1 ] | [ -z "${DOBROHO_VECHORA_ENCRYPTION_PASSWORD}" ]; then
cat
return 0
fi
while IFS="" read -r i || [ -n "${i}" ]
do
echo "${i}"
cipher="$(echo "${i}" | grep -Po 'ENCRYPTED!<\K([a-zA-Z0-9\=\+\/]*)(?=\>)')" || :
if [ -n "${cipher}" ]; then
echo -n "Decrypted: "
echo "${cipher}" | openssl enc -pbkdf2 -d -a -aes256 -pass env:DOBROHO_VECHORA_ENCRYPTION_PASSWORD || :
fi
done
}
print_messages() { print_messages() {
open_socket open_socket
echo -n 2 >&3 echo -n 2 >&3
head -c "${1}" <&3 | sed -e 's/\x1B/ESC/g' head -c "${1}" <&3 | decrypt | sed -e 's/\x1B/ESC/g'
close_socket close_socket
} }
@ -89,18 +113,19 @@ wait_for_messages() {
} }
length=0 length=0
enc=0
while :; do while :; do
read cmd read cmd
case "${cmd}" in case "${cmd}" in
I) I)
tmp="$(mktemp)" tmp="$(mktemp)"
"${VISUAL:-"${EDITOR:-"$(which vi)"}"}" "${tmp}" "${VISUAL:-"${EDITOR:-"$(which vi)"}"}" "${tmp}"
[ -s "${tmp}" ] && send_message 0"${nick}$(cat "${tmp}")" [ -s "${tmp}" ] && send_message 0"${nick}$(cat "${tmp}" | encrypt)"
rm "${tmp}" rm "${tmp}"
;; ;;
i) i)
read a read a
send_message 0"${nick}${a}" send_message 0"${nick}$(echo "${a}" | encrypt)"
;; ;;
L) L)
length="$(get_message 1)" length="$(get_message 1)"
@ -125,6 +150,19 @@ while :; do
q) q)
exit exit
;; ;;
E)
read -s DOBROHO_VECHORA_ENCRYPTION_PASSWORD
export DOBROHO_VECHORA_ENCRYPTION_PASSWORD
;;
e)
if [ "${enc}" -eq 0 ]; then
enc=1
echo 1
else
enc=0
echo 0
fi
;;
*) *)
echo '?' echo '?'
;; ;;