2020-01-29 09:29:08 -05:00
|
|
|
#!/bin/bash
|
|
|
|
set -euo pipefail
|
|
|
|
|
2023-04-14 07:06:14 -04:00
|
|
|
: ${INSTANCE:=https://ots.fyi} # Where to reach the API of the instance (omit trailing slash)
|
|
|
|
|
2020-01-29 09:29:08 -05:00
|
|
|
deps=(curl jq)
|
|
|
|
for cmd in "${deps[@]}"; do
|
2023-04-14 07:06:14 -04:00
|
|
|
which ${cmd} >/dev/null || {
|
|
|
|
echo "'${cmd}' util is required for this script"
|
|
|
|
exit 1
|
|
|
|
}
|
2020-01-29 09:29:08 -05:00
|
|
|
done
|
|
|
|
|
|
|
|
# Get secret from CLI argument
|
|
|
|
SECRET=${1:-}
|
|
|
|
[[ -n $SECRET ]] || {
|
2023-04-14 07:06:14 -04:00
|
|
|
echo "Usage: $0 'secret to share'"
|
|
|
|
exit 1
|
2020-01-29 09:29:08 -05:00
|
|
|
}
|
|
|
|
|
2023-04-14 07:06:14 -04:00
|
|
|
# Generate a random 20 character password
|
|
|
|
pass=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 20 || true)
|
2020-01-29 09:29:08 -05:00
|
|
|
|
|
|
|
# Encrypt the secret
|
2023-04-14 07:06:14 -04:00
|
|
|
ciphertext=$(echo "${SECRET}" | openssl aes-256-cbc -base64 -pass "pass:${pass}" -iter 300000 -md sha512 2>/dev/null)
|
2020-01-29 09:29:08 -05:00
|
|
|
|
|
|
|
# Create a secret and extract the secret ID
|
|
|
|
id=$(
|
2023-04-14 07:06:14 -04:00
|
|
|
curl -sSf \
|
|
|
|
-X POST \
|
|
|
|
-H 'content-type: application/json' \
|
|
|
|
-d "$(jq --arg secret "${ciphertext}" -cn '{"secret": $secret}')" \
|
|
|
|
"${INSTANCE}/api/create" |
|
|
|
|
jq -r '.secret_id'
|
2020-01-29 09:29:08 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
# Display URL to user
|
2023-04-14 07:06:14 -04:00
|
|
|
echo -e "Secret is now available at:\n${INSTANCE}/#${id}%7C${pass}"
|