Why does the base64 of a string contain "\n"?
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT
Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Orient Looping
--
Chapters
00:00 Why Does The Base64 Of A String Contain &Quot;\N&Quot;?
00:17 Accepted Answer Score 367
00:52 Answer 2 Score 85
01:15 Answer 3 Score 5
01:31 Answer 4 Score 6
02:25 Thank you
--
Full question
https://superuser.com/questions/1225134/...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#base64
#avk47
ACCEPTED ANSWER
Score 367
Try:
echo -n "apfjxkic-omyuobwd339805ak:60a06cd2ddfad610b9490d359d605407" | base64 -w 0
From man base64
:
-w
,--wrap=COLS
Wrap encoded lines afterCOLS
character (default76
). Use0
to disable line wrapping.
A likely reason for 76
being the default is that Base64 encoding was to provide a way to include binary files in e-mails and Usenet postings which was intended for humans using monitors with 80 characters width. Having a 76-character width as default made that usecase easier.
ANSWER 2
Score 85
On systems where the -w
option to base64
is not available (e.g. Alpine Linux, an Arch Linux initramfs
hook, etc.), you can manually process the output of base64:
base64 some_file.txt | tr -d \\n
This is the brute-force approach; instead of getting the program to co-operate, I am using tr
to indiscriminately strip every newline on stdout.
ANSWER 3
Score 6
So please use echo -n
to remove the line break before redirecting to base64
; and use base64 -w 0
to prevent base64
itself to add line break into the output.
me@host:~
$ echo -n mypassword | base64 -w 0
bXlwYXNzd29yZA==me@host:~ # <<<<<<<<<<<<<<< notice that no line break added after "==" due to '-w 0'; so me@host is on the same line
$ echo -n 'mypassword' | base64 -w 0
bXlwYXNzd29yZA==me@host:~ # <<<<<<<<<<<<<<<<<< notice adding single quotes does not affect output, so you can use values containing spaces freely
A good way to verify is using od -c
to show the actual chars.
me@host:~
$ echo -n bXlwYXNzd29yZA== | base64 -d | od -c
0000000 m y p a s s w o r d
0000012
You see no "\n" is added. But if you use "echo" without "-n", od
will show "\n":
me@host:~
$ echo mypassword | base64 -w 0
bXlwYXNzd29yZAo=me@host:~
$ echo bXlwYXNzd29yZAo= | base64 -d | od -c
0000000 m y p a s s w o r d \n
0000013
At last, create a function will help you in the future:
base64-encode() {
if [ -z "$@" ]; then
echo "Encode string with base64; echoing without line break, and base64 does not print line break neither, to not introducing extra chars while redirecting. Provide the string to encode. "
return 1
fi
echo -n "$@" | base64 -w 0 # here I suppose string if containing space is already quoted
}
ANSWER 4
Score 5
For anyone using openssl base64
, you can use the -A
flag:
-A Process base64 data on one line (requires -a)
-a Perform base64 encoding/decoding (alias -base64)
The following worked for me:
echo -n '{string}' | openssl base64 -A