case "$1" in
"")
echo "Hello. I will now guide you through this manual Diffie-Hellman key exchange."
g="$DEFAULT_GENERATOR"
m="$DEFAULT_MODULUS"
a="$(gen_key "$m")"
g_a="$(pow "$g" "$a" "$m")"
hash_g_a="$(hfunc "$g_a")"
storage="${DHKX_DIR}/${hash_g_a}"
printf '%s\n%s\n%s\n' "$m" "$g" "$a" > "$storage"
echo "The parameters and key will be stored in '${storage}', but you don't have to write that down."
echo "Give this command to the person you wish to communicate with:"
echo "./dhkx.sh" "a" "$(hex-to-b64 "$m")" "$(hex-to-b64 "$g")" "$(hex-to-b64 "$g_a")"
;;
"a")
shift; m="$(b64-to-hex "$1")"; g="$(b64-to-hex "$2")"; g_a="$(b64-to-hex "$3")"
b="$(gen_key "$m")"
g_b="$(pow "$g" "$b" "$m")"
g_ab="$(pow "$g_a" "$b" "$m")"
echo "Congratulations, your shared key is: $(hfunc "$g_ab")"
echo "Give this command to the person you wish to communicate with:"
hash_g_a="$(hfunc "$g_a")"
echo "./dhkx.sh" "b" "$(hex-to-b64 "$hash_g_a")" "$(hex-to-b64 "$g_b")"
;;
"b")
shift; hash_g_a="$(b64-to-hex "$1")"; g_b="$(b64-to-hex "$2")"
storage="${DHKX_DIR}/${hash_g_a}"
[ -f "$storage" ] || fail "can't find parameters in storage"
{ read m;read g;read a;} < "$storage"
g_ab="$(pow "$g_b" "$a" "$m")"
echo "Congratulations, your shared key is: $(hfunc "$g_ab")"
;;
esac