8

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

1 Answer 1

10

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=='
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! It seems I was experiencing this because I was hardcoding the value of i for testing. But generating it properly works.
Thanks! I spent two days to find the issue of my hash function, I never thought about different result between nodejs and other languages like python/php/perl... We are getting the same with hash crypto.createHash("sha1").update(data, "binary"), I don't understand why we don't have warning in the documentation^^".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.