Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

base64 encode binary string returns different values in NodeJS and Python

I am trying to base64 encode a binary string in NodeJS and python and I'm getting 2 different values.

Note that the value is i is 16 random bytes generated in python using os.urandom(16)

NodeJS

> var i = '>e\x93\x10\xabK\xbe\xfeX\x97\x9a$\r\xef\x8f3';
> var s = new Buffer(i).toString('base64');
> console.log(s);
PmXCkxDCq0vCvsO+WMKXwpokDcOvwo8z

Python

>>> import base64
>>> i = '>e\x93\x10\xabK\xbe\xfeX\x97\x9a$\r\xef\x8f3'
>>> s = base64.b64encode(i)
>>> print s
PmWTEKtLvv5Yl5okDe+PMw==

Am I doing something wrong? It does work for regular string such as my name.

NodeJS

> var s = new Buffer('travis').toString('base64');
undefined
> console.log(s);
dHJhdmlz

Python

>>> s = base64.b64encode('travis')
>>> print s
dHJhdmlz
like image 848
tbeauvais Avatar asked Oct 01 '13 20:10

tbeauvais


People also ask

Is Base64 string always same?

Artjom B. Base64 is not encryption. But yes, different input strings will always encode to different Base64-encoded strings, and the same input string will always encode to the same Base64-encoded string. It's not a hash though, so small changes in the input will only result in small changes in the output.

Is binary and Base64 the same?

Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. The term Base64 originates from a specific MIME content transfer encoding.

What is toString (' Base64 ')?

The first parameter is the data in Base64 and second parameter is "base64". Then you simply have to call "toString" on the buffer object but this time the parameter passed to the method will be "ascii" because this is the data type that you want your Base64 data to convert to.

How do I decode Base64 in node JS?

The Base64 decoding process is very much similar to the encoding process. All you need to do is create a buffer from the Base64 encoding string by using base64 as the second parameter to Buffer. from() and then decode it to the UTF-8 string by using the toString() method.


1 Answers

NodeJS is encoding the UTF-8 representation of the string. Python is encoding the byte string.

In Python, you'd have to do:

>>> i = u'>e\x93\x10\xabK\xbe\xfeX\x97\x9a$\r\xef\x8f3'
>>> i.encode('utf8').encode('base64')
'PmXCkxDCq0vCvsO+WMKXwpokDcOvwo8z\n'

to get the same output.

You created the buffer using a default encoding, which means it interpreted i as UTF-8 to begin with. You need to tell Buffer to treat i as binary instead:

> var i = '>e\x93\x10\xabK\xbe\xfeX\x97\x9a$\r\xef\x8f3';
> var s = new Buffer(i, 'binary').toString('base64');
> s
'PmWTEKtLvv5Yl5okDe+PMw=='
like image 182
Martijn Pieters Avatar answered Oct 14 '22 04:10

Martijn Pieters