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

Base64.decode throws FormatException Invalid length, must be multiple of four #39510

Open
adiospace opened this issue Nov 25, 2019 · 3 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-convert type-enhancement A request for a change that isn't a bug

Comments

@adiospace
Copy link

adiospace commented Nov 25, 2019

Issue with dart:convert

base64.decode or base64Url.decode throws a FormatException('Invalid length, must be multiple of four") when decoding the following base64 encoded string "eyJlbWFpbCI6ImFnb2xhcnVAZ21haWwuY29tIiwibmFtZSI6IkFkcmlhbiBPbGFydSIsInRva2VuIjoiS3FBd2J2M3A0cVZVM3V1Mzg4Z3dqQUVMIiwidXNlcm5hbWUiOiJhZHJpYW5fb2xhcnUiLCJpZCI6NDAyNjIsInJvbGUiOiJ1c2VyIn0".

It works just fine in JavaScript env using atob.

To make it work I had to normalize it first using base64.normalize.

We can close this if it's ok to normalize it explicitly I was just expecting it to work directly using base64.decode method.

Dart VM version: 2.6.0 (Thu Oct 24 17:52:22 2019 +0200) on "macos_x64"

@lrhn
Copy link
Member

lrhn commented Nov 25, 2019

Dart's base64 decoder is deliberately pedantic. That increases the chance that an accidentally truncated input string is detected (it has a 3/4 chance of being invalid).

Maybe we should add an option to accept non-padded data, instead of requiring it to be normalized before decoding.

@lrhn lrhn added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-convert type-enhancement A request for a change that isn't a bug labels Nov 25, 2019
@libill
Copy link

libill commented Feb 28, 2020

when it has non-padded data,we can add padded data(=) before decoder it.

@trailerzone
Copy link

is there a way to handle the error when it is throw?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-convert type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants