pem
Minimal Rust library for parsing and encoding PEM-encoded data
Repository Health
Technical Analysis
pem is a small, focused Rust crate for working with PEM (Privacy-Enhanced Mail) encoded data — the base64-with-header/footer text format used to store X.509 certificates, private keys, and other cryptographic material. It exposes a simple parse/encode API that extracts the tag (e.g. RSA PRIVATE KEY) and raw byte contents from a PEM block, or produces well-formed PEM text from raw bytes.
The crate supports no_std environments via an alloc-only build, optional serde integration for serializing parsed PEM structures, and has accumulated over 256 million total downloads on crates.io, making it one of the most widely depended-upon low-level building blocks in the Rust cryptography ecosystem — used transitively by TLS, certificate, and key-management crates rather than typically depended on directly by application code.
What You Get
- A
parse()/parse_many()API that extracts the tag and decoded bytes from one or more PEM blocks in a string - An
encode()function that serializes raw bytes back into properly line-wrapped PEM text with the correct header/footer no_stdcompatibility (via thealloccrate) for use in embedded or constrained environments- Optional
serdesupport for serializing and deserializing parsedPemstructures - A
Pemstruct exposingtagandcontentsfields for direct inspection of decoded data
Common Use Cases
- Extracting the DER-encoded bytes of a certificate or private key from a PEM file before feeding them into a crypto or TLS library
- Building custom certificate or key management tooling that needs to read or write PEM-formatted files
- Serializing generated keys or certificates back into standard PEM text for storage or transmission
- Embedded or
no_stdprojects that need PEM parsing without the overhead of a full crypto stack
Under The Hood
Architecture: The crate is deliberately small — src/lib.rs (~1,060 lines) defines the public Pem struct and parse/parse_many/encode functions, src/parser.rs (122 lines) contains a minimal hand-written parser for the PEM text format (header line, base64 body, footer line), and src/errors.rs (69 lines) defines a focused error enum. There’s no dependency on a full ASN.1/X.509 parser — pem only handles the outer text envelope, leaving DER interpretation to downstream crates.
Tech Stack: Rust 2021 edition, minimum supported Rust version 1.60.0. Depends only on base64 (with alloc, no std required by default) for the core encode/decode path, and optionally serde_core behind a feature flag. Dev-dependencies include criterion for benchmarking (benches/pem_benchmark.rs) and proptest for property-based testing.
Code Quality: Given the crate’s role as a security-adjacent, extremely widely-depended-upon building block (256M+ downloads), its small surface area and minimal dependency footprint are a deliberate strength — less code and fewer dependencies means a smaller attack surface. Property-based tests via proptest guard the parser against malformed input. Activity has slowed (0.67 commits/month recently) but the crate’s scope is narrow enough that this reflects stability rather than neglect — the format it implements (PEM) has not changed.
API Design: The API surface is intentionally tiny: parse, parse_many, and encode cover the entire use case with no configuration objects or builder patterns needed. This makes it trivial to drop into any project needing PEM handling, at the cost of not exposing lower-level control over encoding parameters (e.g. custom line-wrap width) that some advanced integrations might want.
Used by 3 apps in this directory
LocalSend
Networking
An open-source, cross-platform AirDrop alternative that sends files and messages device-to-device over your local network with no internet, no account, and no cloud server involved.
PeerDB
Data Engineering · Databases
Postgres-native ETL that streams change data capture in real time to Snowflake, BigQuery, ClickHouse, S3, and Kafka — up to 10x faster than general-purpose pipelines, managed through a familiar Postgres SQL interface.
Stalwart
Collaboration
All-in-one secure mail and collaboration server covering IMAP, JMAP, SMTP, CalDAV, CardDAV, and WebDAV in a single memory-safe Rust binary.