base64 encoding in node?

786 views
Skip to first unread message

Marak Squires

unread,
Aug 10, 2010, 4:44:45 PM8/10/10
to nodejs
I've got somewhat of a dumb question, but I haven't been able to get a straight answer about it yet. It's been discussed on the mailing list a few times, but I'm still noobing out here.

Can anyone tell me what the status of getting base64 encoding / decoding into the node core is? Has this already been added? Is it decided this shouldn't be in core?

I ask because I have 3-4 different base64 encode / decode methods spanned across multiple projects and I just got a pull request for yet another one. I see we have http://github.com/pkrumins/node-base64 , but that requires a custom build, which is not ideal.

Are there any consideration to adding this functionality into core? If not, could anyone give me a clear answer why not?


-Marak



Ben Noordhuis

unread,
Aug 10, 2010, 4:55:11 PM8/10/10
to nod...@googlegroups.com
It's part of the core now and fairly stable. Check the docs for the
Buffer API, you can specify 'base64' as an encoding for input and
output.

Marak Squires

unread,
Aug 10, 2010, 5:01:58 PM8/10/10
to nod...@googlegroups.com
Thanks for the quick response!

I was looking at the docs for this a bunch, it just wasn't clicking until I actually tried to repl it a bit. 

Is the proper way to do base64 encoding on a string to convert it into a buffer?

Using something like  var x = new Buffer('foo', 'base64');  ?

If so, is there any point to Peter's module? Does it add any value? Should it be considered depreciated (and possibly removed from npm in the future) ?

-Marak


--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.


Ben Noordhuis

unread,
Aug 10, 2010, 5:21:27 PM8/10/10
to nod...@googlegroups.com
No, that's how you would decode a base64-encoded string into a buffer.
buffer.toString('base64') is what you want, it encodes the content of
the buffer.

As to pkrumin's module being deprecated, you probably should ask him
(I don't think he reads this list). His module may do things the core
doesn't.

>> nodejs+un...@googlegroups.com<nodejs%2Bunsu...@googlegroups.com>

tong

unread,
Aug 11, 2010, 8:33:19 AM8/11/10
to nod...@googlegroups.com
On 08/10/2010 11:21 PM, Ben Noordhuis wrote:
> No, that's how you would decode a base64-encoded string into a buffer.
> buffer.toString('base64') is what you want, it encodes the content of
> the buffer.
>
> As to pkrumin's module being deprecated, you probably should ask him
> (I don't think he reads this list). His module may do things the core
> doesn't.
>

just a note:
the nodejs base64 encoder does NOT fill/remove the null bits (=)
.. which are required basically everywhere.
(most encoder/decoders do, at least the one i've seen)

/tong


--
[) | 5 |< � |2 3 3

Ben Noordhuis

unread,
Aug 11, 2010, 4:48:07 PM8/11/10
to nod...@googlegroups.com
The decoder should (I wrote it). Not sure about the encoder. I'll have
a look tomorrow.

Marak Squires

unread,
Aug 11, 2010, 4:51:34 PM8/11/10
to nod...@googlegroups.com
tmpvar copy pasted me some of the syntax for encoding, it makes sense based on the api docs, but its not very syntax sugary. 

i have the snippets on another machine right, will try to post later

On Wed, Aug 11, 2010 at 4:48 PM, Ben Noordhuis <in...@bnoordhuis.nl> wrote:
The decoder should (I wrote it). Not sure about the encoder. I'll have
a look tomorrow.

--

Ben Noordhuis

unread,
Aug 12, 2010, 3:23:39 PM8/12/10
to nod...@googlegroups.com
On Wed, Aug 11, 2010 at 14:33, tong <tong.d...@gmail.com> wrote:
> the nodejs base64 encoder does NOT fill/remove the null bits (=)
> .. which are required basically everywhere.

Just checked, both the encoder and the decoder work as expected. Do
you perhaps have a test case?

Elijah Insua

unread,
Aug 12, 2010, 4:20:30 PM8/12/10
to nod...@googlegroups.com

tong

unread,
Aug 13, 2010, 5:09:41 AM8/13/10
to nod...@googlegroups.com
nothing extracted .. but 2 days ago i switched from:
http://github.com/brainfucker/node-base64
.. to the the built-in decoder, which broke authentication process stuff
on my XMPP server(which depends on decoding base64) until i removed the
null bits manually.

the line i am talking about is here:
http://disktree.spektral.at/git/?a=viewblob&p=owl&h=0bea3576035ec71aa6703e2f8ce881b823381a66&hb=d4235be9b501bcce478ae31542bff56fd90070d4&f=owl/client/sasl/PlainMechanism.hx

.. in the 'start' method.

and the 'removeNullbits' method is here:
http://disktree.spektral.at/git/?a=viewblob&p=owl&h=4a215e9d9ed03988c268cb8d503b709e30eea6a3&hb=d4235be9b501bcce478ae31542bff56fd90070d4&f=owl/util/Base64.hx

... this is haXe code compiled to javascript, but this shouldn't make
any difference.
if you sure your decoding is correct the problem might be somewhere
else, but as said it worked before using the built-in one.

best.tong

Ben Noordhuis

unread,
Aug 13, 2010, 9:26:33 AM8/13/10
to nod...@googlegroups.com
tong, have you tested this against the latest and greatest from Ryan's
GH repository? The current decoder is only two weeks old, it probably
hasn't made it into any stable releases yet.

http://github.com/ry/node/blob/cf909e8725f12fa2f6356727cdae3110dd388c18/test/simple/test-buffer.js#L265

All tests that deal with padding pass but perhaps they're not rigid
enough. Could you post a few example inputs and expected outputs? That
would really help.

tong

unread,
Aug 14, 2010, 6:26:18 AM8/14/10
to nod...@googlegroups.com
.. big sorry! .. i was wrong. decoding works as expected.
the error is in my websocket connection, everything is fine with
'normal' type sockets.
(very strange my manual removeNullbits method solved this for the
websocket # )

thanks/tong.

Ben Noordhuis

unread,
Aug 14, 2010, 3:00:43 PM8/14/10
to nod...@googlegroups.com
On Sat, Aug 14, 2010 at 12:26, tong <tong.d...@gmail.com> wrote:
> .. big sorry! .. i was wrong. decoding works as expected.
> the error is in my websocket connection, everything is fine with 'normal'
> type sockets.
> (very strange my manual removeNullbits method solved this for the websocket
> # )

Don't worry, tong. Glad you got it sorted out. :-)

Reply all
Reply to author
Forward
0 new messages