Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buffer Base64 Encoding Truncates or Adds Extra Characters #6107

Closed
yunong opened this issue Apr 7, 2016 · 4 comments
Closed

Buffer Base64 Encoding Truncates or Adds Extra Characters #6107

yunong opened this issue Apr 7, 2016 · 4 comments
Labels
buffer Issues and PRs related to the buffer subsystem. invalid Issues and PRs that are invalid.

Comments

@yunong
Copy link
Member

yunong commented Apr 7, 2016

  • Version: 0.12-5.x
  • Platform: Mac OS X El Capitan
  • Subsystem: Buffer

Encoding and Decoding a new Buffer object with base64 results in garbled or missing data.

> new Buffer('hello', 'base64').toString('base64');
'hell'
> new Buffer('helloIsItMeYourLookingFor', 'base64').toString('base64');
'helloIsItMeYourLookingFo'
> new Buffer('helloIsItMe', 'base64').toString('base64');
'helloIsItMc='
>

Notice how in the first and second example, the last character is missing, and in the third example, there are extra characters appended to the output.

cc/ @trevnorris

@mscdex mscdex added the buffer Issues and PRs related to the buffer subsystem. label Apr 7, 2016
@kzc
Copy link

kzc commented Apr 7, 2016

You're converting ill-formed base64 input to base64. Try this instead:

Buffer(Buffer('hello', 'utf8').toString('base64'), 'base64').toString();

@mscdex mscdex added the question Issues that look for answers. label Apr 7, 2016
@mscdex
Copy link
Contributor

mscdex commented Apr 7, 2016

@kzc is right, the second argument passed to Buffer() is the encoding for the string passed in as the first argument. 'helloIsItMeYourLookingFor' is not base64-encoded.

@mscdex mscdex added invalid Issues and PRs that are invalid. and removed question Issues that look for answers. labels Apr 7, 2016
@mscdex mscdex closed this as completed Apr 7, 2016
@davepacheco
Copy link
Contributor

Shouldn't invalid input produce an error rather than garbage output?

@addaleax
Copy link
Member

addaleax commented Apr 8, 2016

Base64 comes in various forms, the most common using one or two = signs as a form of final padding:

Buffer.from([0x01, 0x02, 0x03, 0x04]).toString('base64') === 'AQIDBA=='

This padding makes a buffer encoded with this variant of Base64 always have a length that is a multiple of 4, which is a nice property to have when writing a decoder for it.
However, it’s not strictly necessary – there is no additional information in those = characters that wouldn’t also be conveyed by the length of the encoded string.

So, whether you should consider this input invalid is a question you can probably argue about a lot. I’d say there’s no real downside to accepting it, especially since there are some uncommon Base64 encoders which actually leave out the padding; Wikipedia has a quite comprehensive listing of the different variants out there.

@jspri jspri mentioned this issue May 17, 2016
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
buffer Issues and PRs related to the buffer subsystem. invalid Issues and PRs that are invalid.
Projects
None yet
Development

No branches or pull requests

5 participants