base64-js icon indicating copy to clipboard operation
base64-js copied to clipboard

Ignore padding on decode

Open oliverjanik opened this issue 6 years ago • 2 comments

Strictly speaking the '=' characters do not carry any information. Can decode ignore missing padding and not throw an error?

Hello downvoters, care to explain your reasoning?

oliverjanik avatar Jul 11 '18 05:07 oliverjanik

From those lines in the code seems URL-safe base64 strings are supported, but if padding is required then those aren't supported as some variants don't use it.

Node supports decoding URL-safe base64 strings without padding. Example: Buffer.from('HI', 'base64') return "<Buffer 1c>". base64js.toByteArray('HI') throws "Invalid string. Length must be a multiple of 4".

qgustavor avatar Nov 08 '18 21:11 qgustavor

As a workaround, I re-inject any missing padding before passing the text to this library, like so -

let b64 = "AAAAAAA"; // base64-encoded text that could be missing padding
if( (b64.length % 4) !== 0) b64 += "=".repeat(4 - (b64.length % 4))

eugene1g avatar Mar 13 '22 14:03 eugene1g