This repository has been archived by the owner on Jan 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgked.sh
executable file
·52 lines (43 loc) · 1.51 KB
/
gked.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/bash
helpFunction()
{
echo ""
echo "Usage: $0 -a encrypt -t 'pass@1234'"
echo " -a action (ex. encrypt or decrypt)"
echo " -t plaintext or chipertext"
exit 1 # Exit script after printing help
}
while getopts "a:t:" opt
do
case "$opt" in
a ) parameterA="$OPTARG" ;;
t ) parameterT="$OPTARG" ;;
? ) helpFunction ;; # Print helpFunction in case parameter is non-existent
esac
done
if [ -z "$parameterA" ] || [ -z "$parameterT" ]
then
echo "Some or all of the parameters are empty";
helpFunction
fi
keyring=`gcloud kms keyrings list --location=global --format=json | jq .[0].name -r`
key=`gcloud kms keys list --keyring=$keyring --location=global --format=json | jq .[0].name -r`
if [ "encrypt" = "${parameterA,,}" ]; then
PLAINTEXT=$parameterT
echo "Encrypting Text: $PLAINTEXT"
ENCRYPTED=`curl -s "https://cloudkms.googleapis.com/v1/$key:encrypt" \
-d "{\"plaintext\":\"$(echo -n $PLAINTEXT | base64 -w 0)\"}" \
-H "Authorization:Bearer $(gcloud auth application-default print-access-token)"\
-H "Content-Type:application/json" \
| jq .ciphertext -r`
echo "Base64 Encrypted Text: $ENCRYPTED"
fi
if [ "decrypt" == "${parameterA,,}" ]; then
ENCRYPTED_TEXT=$parameterT
DECRYPTED=`curl -s "https://cloudkms.googleapis.com/v1/$key:decrypt" \
-d "{\"ciphertext\":\"$ENCRYPTED_TEXT\"}" \
-H "Authorization:Bearer $(gcloud auth application-default print-access-token)"\
-H "Content-Type:application/json" \
| jq .plaintext -r`
echo "Decrypted Text: $(echo $DECRYPTED | base64 -d)"
fi