0

I need to convert the contents of the variables as they are into base64. I decided to make a function and pass the content of the variables into it and output it later. So far I have the following

#!/bin/dash

payload1='{
  "currency": "ABC",
  "transaction": "1-8ef2b11f1b"
}
'

payload2='{x}'

function base64_encode() {
# must first receive the data
# ...
  printf '%s' "${input}" | base64 | tr -d '=' | tr '/+' '_-' | tr -d '\n'
}

# something like:
printf '%s' "$(printf '%s' "${payload1}" | base64_encode)"
printf '%s' "$(printf '%s' "${payload2}" | base64_encode)"

The bottom line is that I should get exactly ewogICJjdXJyZW5jeSI6ICJBQkMiLAogICJ0cmFuc2FjdGlvbiI6ICIxLThlZjJiMTFmMWIiCn0K, not ewogICJjdXJyZW5jeSI6ICJBQkMiLAogICJ0cmFuc2FjdGlvbiI6ICIxLThlZjJiMTFmMWIiCn0, from payload1 variable. The problem is that I don't know how to put the contents of variable in base64_encode function.

Is there any universal way to do this? Or maybe my approach is generally wrong?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
KarlsD
  • 649
  • 1
  • 6
  • 12
  • `function funcname() {` is not POSIX-compliant, and I'm very surprised that dash has started accepting it. The POSIX-standard way to declare a function is just `funcname() {` with no preceding `function`. See https://wiki.bash-hackers.org/scripting/obsolete – Charles Duffy Dec 02 '21 at 18:33
  • BTW, do you _really_ need sh/dash compatibility here? The easy ways to make this code POSIX-compliant tend to throw away trailing newlines (reading to a variable from stdin with `input=$(cat)` or any other command substitution discards them) – Charles Duffy Dec 02 '21 at 18:36
  • 2
    ...on a different topic -- it would be helpful if you described how someone can convince themselves that the output with the trailing `K` is correct, instead of just asserting it without an explanation. – Charles Duffy Dec 02 '21 at 18:40
  • `dash` doesn't accept `function` (at least, the version I compiled from source just now doesn't). I suspect a typo for `#!/bin/bash`. – chepner Dec 02 '21 at 18:45
  • 1
    Don't remove the trailing `=` signs, they're important: https://en.wikipedia.org/wiki/Base64#Output_padding – glenn jackman Dec 02 '21 at 21:38
  • 1
    Also, those `tr` commands will render the encoded string un-decodable: underscore and minus are not legal base64 characters: https://en.wikipedia.org/wiki/Base64#Base64_table – glenn jackman Dec 02 '21 at 21:42

1 Answers1

0

Since you're piping data into the function, let base64 read directly from stdin:

base64_encode() {
  base64    # removed questionable `tr` commands
}
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • `tr -d '\n'` is necessary because GNU `base64` adds newlines every 76 characters. The other `tr` are for converting `base64` to `base64 URL`. – Fravadona Dec 02 '21 at 22:20
  • ...GNU base64 only does that unless it's told not to, which it very much can be. `base64 -w 0` and there you are. – Charles Duffy Dec 02 '21 at 22:38
  • Yes, but `base64` on other OSs don't add those newlines (at least BSD and Solaris), so a _portable_ solution is to `tr -d '\n'` – Fravadona Dec 02 '21 at 22:41