getBytesFromPEMString static method

Uint8List getBytesFromPEMString(
  1. String pem
)

Helper function for decoding the base64 in pem.

Throws an ArgumentError if the given pem is not sourounded by begin marker -----BEGIN and endmarker -----END or the pem consists of less than two lines.

Implementation

static Uint8List getBytesFromPEMString(String pem) {
  var lines = LineSplitter.split(pem)
      .map((line) => line.trim())
      .where((line) => line.isNotEmpty)
      .toList();

  if (lines.length < 2 ||
      !lines.first.startsWith('-----BEGIN') ||
      !lines.last.startsWith('-----END')) {
    throw ArgumentError('The given string does not have the correct '
        'begin/end markers expected in a PEM file.');
  }
  var base64 = lines.sublist(1, lines.length - 1).join('');
  return Uint8List.fromList(base64Decode(base64));
}