pem library

Utilities for decoding/encoding PEM as specified in RFC 7468.

Decoding methods in this package follow RFC 7468 and ignore text around the PEM block. This package does not attempt to distinguish between malformed PEM blocks and surrounding text that should be ignored. Thus, no decoding method in this package will throw exceptions for malformed PEM blocks, the PemCodec will however throw if there is no PEM blocks present.

By default decoding methods in this package ignore extra whitespace, tabs, and line breaks as specified for lax-mode in RFC 7468. While encoding always produces strict-mode output. This aims to maximize interoperability with minimal risks of misinterpreting the input.

To avoid inappropriate confusion of different types of entities decoding methods in this package requires a PemLabel to be specified, and ignores PEM blocks without matching labels. A flag unsafeIgnoreLabel can be provided to these methods to ignore the labels, however, this is strongly discouraged.

Example

import 'package:pem/pem.dart';

// Parse PEM encoded private key.
List<int> keyData = PemCodec(PemLabel.privateKey).decode("""
  -----BEGIN PRIVATE KEY-----
  MIGEAgEAMBAGByqGSM49AgEGBSuBBAAKBG0wawIBAQQgVcB/UNPxalR9zDYAjQIf
  jojUDiQuGnSJrFEEzZPT/92hRANCAASc7UJtgnF/abqWM60T3XNJEzBv5ez9TdwK
  H0M6xpM2q+53wmsN/eYLdgtjgBd3DBmHtPilCkiFICXyaA8z9LkJ
  -----END PRIVATE KEY-----
""");

// Encode keyData as PEM string.
String pemBlock = PemCodec(PemLabel.privateKey).encode(keyData);

// Print encoded block (should print what we parsed, without indentation)
print(pemBlock);

To parse a series of concatenated PEM strings as often happens when giving a list of certificates see decodePemBlocks.

Classes

PemCodec
A Codec for encoding and decoding PEM blocks with a given PemLabel.

Enums

PemLabel
Labels for PEM encoded strings.

Functions

decodePemBlocks(PemLabel label, String pemString, {bool strict = false, bool unsafeIgnoreLabel = false}) List<List<int>>
Decode a String of one or more concatenated PEM blocks.
encodePemBlock(PemLabel label, List<int> data) String
Encode data as a PEM block with the given label.

Exceptions / Errors

NoPemBlockFoundException
Thrown by PemCodec if no valid PEM blocks was found during decoding.