Expand description
This crate provides a parser and encoder for PEM-encoded binary data. PEM-encoded binary data is essentially a beginning and matching end tag that encloses base64-encoded binary data (see: https://en.wikipedia.org/wiki/Privacy-enhanced_Electronic_Mail).
This crate’s documentation provides a few simple examples along with documentation on the public methods for the crate.
Usage
This crate is on crates.io and can be used
by adding pem
to your dependencies in your project’s Cargo.toml
.
[dependencies]
pem = "2"
Using the serde
feature will implement the serde traits for
the Pem
struct.
Example: parse a single chunk of PEM-encoded text
Generally, PEM-encoded files contain a single chunk of PEM-encoded text. Commonly, this is in some sort of a key file or an x.509 certificate.
use pem::parse;
const SAMPLE: &'static str = "-----BEGIN RSA PRIVATE KEY-----
MIIBPQIBAAJBAOsfi5AGYhdRs/x6q5H7kScxA0Kzzqe6WI6gf6+tc6IvKQJo5rQc
dWWSQ0nRGt2hOPDO+35NKhQEjBQxPh/v7n0CAwEAAQJBAOGaBAyuw0ICyENy5NsO
2gkT00AWTSzM9Zns0HedY31yEabkuFvrMCHjscEF7u3Y6PB7An3IzooBHchsFDei
AAECIQD/JahddzR5K3A6rzTidmAf1PBtqi7296EnWv8WvpfAAQIhAOvowIXZI4Un
DXjgZ9ekuUjZN+GUQRAVlkEEohGLVy59AiEA90VtqDdQuWWpvJX0cM08V10tLXrT
TTGsEtITid1ogAECIQDAaFl90ZgS5cMrL3wCeatVKzVUmuJmB/VAmlLFFGzK0QIh
ANJGc7AFk4fyFD/OezhwGHbWmo/S+bfeAiIh2Ss2FxKJ
-----END RSA PRIVATE KEY-----
";
let pem = parse(SAMPLE).unwrap();
assert_eq!(pem.tag(), "RSA PRIVATE KEY");
Example: parse a set of PEM-encoded test
Sometimes, PEM-encoded files contain multiple chunks of PEM-encoded text. You might see this if you have an x.509 certificate file that also includes intermediate certificates.
use pem::parse_many;
const SAMPLE: &'static str = "-----BEGIN INTERMEDIATE CERT-----
MIIBPQIBAAJBAOsfi5AGYhdRs/x6q5H7kScxA0Kzzqe6WI6gf6+tc6IvKQJo5rQc
dWWSQ0nRGt2hOPDO+35NKhQEjBQxPh/v7n0CAwEAAQJBAOGaBAyuw0ICyENy5NsO
2gkT00AWTSzM9Zns0HedY31yEabkuFvrMCHjscEF7u3Y6PB7An3IzooBHchsFDei
AAECIQD/JahddzR5K3A6rzTidmAf1PBtqi7296EnWv8WvpfAAQIhAOvowIXZI4Un
DXjgZ9ekuUjZN+GUQRAVlkEEohGLVy59AiEA90VtqDdQuWWpvJX0cM08V10tLXrT
TTGsEtITid1ogAECIQDAaFl90ZgS5cMrL3wCeatVKzVUmuJmB/VAmlLFFGzK0QIh
ANJGc7AFk4fyFD/OezhwGHbWmo/S+bfeAiIh2Ss2FxKJ
-----END INTERMEDIATE CERT-----
-----BEGIN CERTIFICATE-----
MIIBPQIBAAJBAOsfi5AGYhdRs/x6q5H7kScxA0Kzzqe6WI6gf6+tc6IvKQJo5rQc
dWWSQ0nRGt2hOPDO+35NKhQEjBQxPh/v7n0CAwEAAQJBAOGaBAyuw0ICyENy5NsO
2gkT00AWTSzM9Zns0HedY31yEabkuFvrMCHjscEF7u3Y6PB7An3IzooBHchsFDei
AAECIQD/JahddzR5K3A6rzTidmAf1PBtqi7296EnWv8WvpfAAQIhAOvowIXZI4Un
DXjgZ9ekuUjZN+GUQRAVlkEEohGLVy59AiEA90VtqDdQuWWpvJX0cM08V10tLXrT
TTGsEtITid1ogAECIQDAaFl90ZgS5cMrL3wCeatVKzVUmuJmB/VAmlLFFGzK0QIh
ANJGc7AFk4fyFD/OezhwGHbWmo/S+bfeAiIh2Ss2FxKJ
-----END CERTIFICATE-----
";
let pems = parse_many(SAMPLE).unwrap();
assert_eq!(pems.len(), 2);
assert_eq!(pems[0].tag(), "INTERMEDIATE CERT");
assert_eq!(pems[1].tag(), "CERTIFICATE");
Features
This crate supports two features: std
and serde
.
The std
feature is enabled by default. If you specify
default-features = false
to disable std
, be aware that
this crate still needs an allocator.
The serde
feature implements serde::{Deserialize, Serialize}
for this crate’s Pem
struct.
Macros
Structs
- Configuration for Pem encoding
- Provides access to the headers that might be found in a Pem-encoded file
- Iterator across all headers in the Pem-encoded data
- A representation of Pem-encoded data
Enums
- Enum describing line endings
- The
pem
error type.
Functions
- Encode a PEM struct into a PEM-encoded data string
- Encode a PEM struct into a PEM-encoded data string with additional configuration options
- Encode multiple PEM structs into a PEM-encoded data string
- Encode multiple PEM structs into a PEM-encoded data string with additional configuration options
- Parses a single PEM-encoded data from a data-type that can be dereferenced as a u8.
- Parses a set of PEM-encoded data from a data-type that can be dereferenced as a u8.
Type Definitions
- The
pem
result type.