LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-02-2016, 01:23 PM   #1
0x53h
LQ Newbie
 
Registered: May 2012
Posts: 23

Rep: Reputation: Disabled
base64 encoding in program vs piping to base64 command-line utility


What's going on when I pipe a string into base64 on the command line vs base64 encoding in perl or python?

Code:
user@box:~/$ perl -MMIME::Base64 -e 'print encode_base64("user\@example.com\0user\@example.com\0test")';                                                                                                 
dmljdG9yQHc2LmlvAHZpY3RvckB3Ni5pbwB0ZXN0

user@box:~/$ python -c "import base64; print(base64.b64encode('user@example.com\00user@example.com\00test'))"
dmljdG9yQHc2LmlvAHZpY3RvckB3Ni5pbwB0ZXN0

user@box:~/$ python -c "print('user@example.com\00user@example.com\00test').strip()" | base64                                                                                                      
dmljdG9yQHc2LmlvAHZpY3RvckB3Ni5pbwB0ZXN0Cg==
 
Old 08-03-2016, 04:54 PM   #2
Rinndalir
Member
 
Registered: Sep 2015
Posts: 733

Rep: Reputation: Disabled
Look at python -m pydoc print

Code:
A "'\n'" character is written at the end, unless the "print" statement
ends with a comma.
 
Old 08-04-2016, 01:01 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I knew that rang a bell, just couldn't remember until Rinndalir posted.

I came across the same thing a while back in bash
Code:
echo aaa |base64
YWFhCg==

echo -n aaa |base64
YWFh

Last edited by chrism01; 08-08-2016 at 01:06 AM. Reason: typo
 
Old 08-05-2016, 10:39 AM   #4
0x53h
LQ Newbie
 
Registered: May 2012
Posts: 23

Original Poster
Rep: Reputation: Disabled
I see. I don't understand how to provide a coma at the end of the print statement. It can be done in Python2 with a future import making that line a bit awkard to type. Thanks though, this clears up the mystery. I believed .strip() would have taken care of that "\n".

Code:
$ python -c "from __future__ import print_function; print('user@example.com\00user@example.com\00test', end='')" | base64
dXNlckBleGFtcGxlLmNvbQB1c2VyQGV4YW1wbGUuY29tAHRlc3Q=
TIL: python -m pydoc foo and why so many base64 encoded strings end in ==
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
program: Base64 timinator Other *NIX 1 07-16-2013 12:27 AM
Can I use base64 as a hash function? fantasy1215 Programming 8 01-13-2013 01:04 AM
[SOLVED] A question about generating base64 strings... trist007 Linux - Newbie 2 03-16-2011 01:21 PM
BASE64 APIs? EddyHahn Programming 0 11-29-2004 09:59 PM
Recommended XML / Base64 encoding libraries? overbored Programming 2 10-14-2004 11:21 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration