18

How to native convert string -> base64 and base64 -> string

I'm find only this bytes to base64string

would like this:

String Base64String.encode();
String Base64String.decode();

or ported from another language is easier?

live-love
  • 48,840
  • 22
  • 240
  • 204
Sergey Karasev
  • 4,513
  • 4
  • 25
  • 24

5 Answers5

13

As of 0.9.2 of the crypto package

CryptoUtils is deprecated. Use the Base64 APIs in dart:convert and the hex APIs in the convert package instead.

import 'dart:convert' show utf8, base64;

main() {
  final str = 'https://dartpad.dartlang.org/';

  final encoded = base64.encode(UTF8.encode(str));
  print('base64: $encoded');

  final str2 = utf8.decode(base64.decode(encoded));
  print(str2);
  print(str == str2);
}

Try it in DartPad

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
7

You can use the BASE64 codec (renamed base64 in Dart 2) and the LATIN1 codec (renamed latin1 in Dart 2) from convert library.

var stringToEncode = 'Dart is awesome';

// encoding

var bytesInLatin1 = LATIN1.encode(stringToEncode);
// [68, 97, 114, 116, 32, 105, 115, 32, 97, 119, 101, 115, 111, 109, 101]

var base64encoded = BASE64.encode(bytesInLatin1);
// RGFydCBpcyBhd2Vzb21l

// decoding

var bytesInLatin1_decoded = BASE64.decode(base64encoded);
// [68, 97, 114, 116, 32, 105, 115, 32, 97, 119, 101, 115, 111, 109, 101]

var initialValue = LATIN1.decode(bytesInLatin1_decoded);
// Dart is awesome

If you always use LATIN1 to generate the encoded String you can avoid the 2 convert calls by creating a codec to directly convert a String to/from an encoded String.

var codec = LATIN1.fuse(BASE64);

print(codec.encode('Dart is awesome'));
// RGFydCBpcyBhd2Vzb21l

print(codec.decode('RGFydCBpcyBhd2Vzb21l'));
// Dart is awesome
Alexandre Ardhuin
  • 71,959
  • 15
  • 151
  • 132
7

I would comment on Günter's April 10th, 2016, post, but I don't have the reputation. As he says, you should use the dart:convert library, now. You have to combine a couple of codecs to get a utf8 string out of a base64 string and vice-versa. This article says that fusing your codecs is faster.

import 'dart:convert';

void main() {
  var base64 = 'QXdlc29tZSE=';
  var utf8 = 'Awesome!';

  // Combining the codecs
  print(utf8 == UTF8.decode(BASE64.decode(base64)));
  print(base64 == BASE64.encode(UTF8.encode(utf8)));
  // Output:
  // true
  // true

  // Fusing is faster, and you don't have to worry about reversing your codecs
  print(utf8 == UTF8.fuse(BASE64).decode(base64));
  print(base64 == UTF8.fuse(BASE64).encode(utf8));
  // Output:
  // true
  // true
}

https://dartpad.dartlang.org/5c0e1cfb6d1d640cdc902fe57a2a687d

exavatar
  • 259
  • 1
  • 3
  • 10
  • Why isn't this built into dart:convert library?! – Pat Jan 12 '17 at 21:33
  • It's not built into the convert library because that library has a lot of different converters, encoders and codecs, and it would be overkill to have all the combinations of these as well. The `fuse` operation is there so you can make the combinations you neeed yourself. – lrn Jul 02 '18 at 05:34
  • this does not work for me, my string is binary, I can't do a utf8 decode on it – Mathieu J. Jul 02 '18 at 09:52
  • I created this if you are using DartPad and having problems to render an app with the long string variable https://github.com/dart-lang/dart-pad/issues/1342#issuecomment-862016884 – Marcello DeSales Jun 16 '21 at 04:12
4

Here is a example of encoding/decoding in dart:

main.dart:

import 'dart:convert';

main() {
  // encode
  var str = "Hello world";
  var bytes = utf8.encode(str);
  var base64Str = base64.encode(bytes);
  print(base64Str);

  // decode
  var decoded_bytes = base64.decode(base64Str);
  var decoded_str = utf8.decode(decoded_bytes);
  print(decoded_str);
}
live-love
  • 48,840
  • 22
  • 240
  • 204
1

I took a class dart.io -> base64.dart, modified it a bit, and there you are

how to use:

var somestring = 'Hello dart!';

var base64string = Base64String.encode( somestring );
var mystring = Base64String.decode( base64string );

source on pastbin.com

source on gist.github.com

Sergey Karasev
  • 4,513
  • 4
  • 25
  • 24