How to properly encode string based on json file?
2
votes
2
answers
11851
views
I have the following
basic.json
file:
{
"user": "user",
"pass": "password"
}
I'm trying to encode it in base64 like this:
"Basic dXNlcjpwYXNzd29yZA=="
I think I'm close:
echo Basic $(echo $(cat basic.json | jq '.user'):$(cat basic.json | jq '.pass') | base64)
I've used the [jq access method found here](https://shapeshed.com/jq-json/#how-to-find-a-key-and-value) .
I've used the [base64 method found here](https://linuxhint.com/bash_base64_encode_decode/) .
But the result is wrong:
Basic InVzZXIiOiJwYXNzd29yZCIK
I've tried the -e
flag as mentioned in the article:
echo Basic $(echo $(cat basic.json | jq '.user'):$(cat basic.json | jq '.pass') | base64 -e)
But it throws this error:
> base64: invalid option -- 'e'
Where did I mistake?
Thanks in advance.
---
**My solution**
The command ended this way:
RUN echo "map \"\" \$basicAuth {\n\
\tdefault $(jq '"Basic " + ("\(.user):\(.pass)" | @base64)' basic.json);\n\
}" > basic.conf
And my basic.conf
file finally have the correct basic auth:
map "" $basicAuth {
default "Basic dXNlcjpwYXNzd29yZA==";
}
Thank you all
Asked by Washington Guedes
(133 rep)
Jun 1, 2020, 02:01 PM
Last activity: Jun 3, 2024, 08:27 AM
Last activity: Jun 3, 2024, 08:27 AM