-
Notifications
You must be signed in to change notification settings - Fork 30.5k
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
Comments
You're converting ill-formed base64 input to base64. Try this instead:
|
@kzc is right, the second argument passed to |
Shouldn't invalid input produce an error rather than garbage output? |
Base64 comes in various forms, the most common using one or two 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. 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. |
Encoding and Decoding a new
Buffer
object withbase64
results in garbled or missing data.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
The text was updated successfully, but these errors were encountered: