pem 1.0.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 78

PEM Encoding/Decoding for Dart #

Encoding/decoding of PEM textual key encoding following RFC 7468.

Disclaimer: This is not an officially supported Google product.

To maximize interoperabilty encoding methods in this package always produces strict-mode output. While decoding methods defaults to lax-mode which ignores extra whitespace, line breaks, tabs as specified in RFC 7468.

Decoding methods ignore text surrounding the PEM blocks, this also implies that decoding methods cannot distinguish between malformed PEM blocks and text to be ignored. Thus, malformed PEM blocks will not cause exceptions to be thrown, though the PemCodec will throw if no PEM block with acceptable label is present.

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);

See API reference for further details and examples on how to parse documents containing multiple PEM blocks, as is often the case for certificate chains.

v1.0.0 #

  • Initial release, with support for encoding/decoding PEM blocks.

example/main.dart

// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import 'package:pem/pem.dart';

void main() {
  // Parse private key from PEM string.
  final privateKeydata = PemCodec(PemLabel.privateKey).decode("""
    -----BEGIN PRIVATE KEY-----
    MIGEAgEAMBAGByqGSM49AgEGBSuBBAAKBG0wawIBAQQgVcB/UNPxalR9zDYAjQIf
    jojUDiQuGnSJrFEEzZPT/92hRANCAASc7UJtgnF/abqWM60T3XNJEzBv5ez9TdwK
    H0M6xpM2q+53wmsN/eYLdgtjgBd3DBmHtPilCkiFICXyaA8z9LkJ
    -----END PRIVATE KEY-----
  """);

  // Print number of bytes in the key, we could obviously also pass it to
  // another library to use the key.
  print('private key contains ${privateKeydata.length} bytes');
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  pem: ^1.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:pem/pem.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
57
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
78
Learn more about scoring.

We analyzed this package on Nov 8, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.0
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:pem/pem.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
petitparser ^2.1.1 2.4.0
Transitive dependencies
meta 1.1.8
Dev dependencies
pedantic ^1.4.0
test ^1.5.1