Sdk: Base64.decode throws FormatException Invalid length, must be multiple of four

Created on 25 Nov 2019  路  2Comments  路  Source: dart-lang/sdk

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"

area-library library-convert type-enhancement

Most helpful comment

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.

All 2 comments

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.

when it has non-padded data锛寃e can add padded data(=) before decoder it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bergwerf picture bergwerf  路  3Comments

DartBot picture DartBot  路  3Comments

emilniklas picture emilniklas  路  3Comments

brooth picture brooth  路  3Comments

xster picture xster  路  3Comments