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