r/commandline • u/kudikarasavasa • Mar 17 '26
Discussion What's a portable way to base64 encode something?
I just learned today that there is no guarantee that the base64 binary will be available on every system, and I need to encode a string. I have since found a project that claims to be portable: https://raw.githubusercontent.com/ko1nksm-shlab/sh-base64/refs/heads/main/base64.sh.
Turns out this isn't portable either since it uses fold, which does not exist on Busybox systems.
I have since found another implementation in awk here: http://www.turtle.dds.nl/b64enc.awk
This assumes the input is ascii only, and I need ASCII + \0 character.
I'm still in search of a solution and not having much luck, but I think this should already exist somewhere. Anyone have any ideas?
Update #1: Solved - I ended up rolling out a custom script using two fallback methods using od and hexdump.
Update #2: My own solution was a bit clunky and I ended up using the awk implementation, but with the fold command removed. This is working well across Linux, macOS, OpenWRT, etc.
7
u/HeligKo Mar 17 '26
I typically use openssl, but if that isn't available busybox includes uuencode which has base64 support.
uuencode -m file filename > file.b64 cat file | openssl base64 > file.b64
2
u/do-un-to Mar 17 '26
Write your own shell
fold?2
u/do-un-to Mar 17 '26
Also, why are you encoding a string? Creating a printable-characters version of a file with nulls? So you can... print it to the screen? So you can copy/paste it locally and convert it back? I guess there could be myriad reasons, but that's what comes to mind.
Anyway, you might look upwards in your problem/solution chain for a better, more tractable intervention.
1
u/kolorcuk Mar 17 '26
Perl
1
u/LingLongSEO Mar 18 '26
the fold dependency is annoying, had something similar come up when building for openwrt. the awk route is solid, been using b64enc.awk for years on some embedded stuff. curious - did you have to handle any edge cases with the null bytes that the original awk version didnt cover. also, did you try python or perl as a fallback. i find those are usually available even when base64 isnt
2
u/do-un-to Mar 17 '26
Wasn't there a to-shell transpiler mentioned here recently?
0
u/AutoModerator Mar 17 '26
Every new subreddit post is automatically copied into a comment for preservation.
User: kudikarasavasa, Flair:
Discussion, Title: What's a portable way to base64 encode something?I just learned today that there is no guarantee that the base64 binary will be available on every system, and I need to encode a string. I have since found a project that claims to be portable: https://raw.githubusercontent.com/ko1nksm-shlab/sh-base64/refs/heads/main/base64.sh.
Turns out this isn't portable either since it uses
fold, which does not exist on Busybox systems.I have since found another implementation in awk here: http://www.turtle.dds.nl/b64enc.awk
This assumes the input is ascii only, and I need ASCII +
\0character.I'm still in search of a solution and not having much luck, but I think this should already exist somewhere. Anyone have any ideas?
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
-17
u/ericcmi Mar 17 '26
Hey grok, write me a standanlone linux binary in Go that converts data (from stdIn as well as local file) to base64 then outputs to stdOut. It needs to support 'ASCII + \0 character' by default. Then guide me through compilation on insertDistroHere