gpg_inspector_lib/lib.rs
1//! A library for parsing and inspecting OpenPGP (GPG) packets.
2//!
3//! This crate provides tools for decoding ASCII-armored PGP data and parsing
4//! the binary packet structure according to RFC 4880 and RFC 9580.
5//!
6//! # Quick Start
7//!
8//! ```
9//! use gpg_inspector_lib::{parse, Packet, PacketBody};
10//!
11//! let armored_key = r#"-----BEGIN PGP PUBLIC KEY BLOCK-----
12//!
13//! mDMEZ... (your armored data)
14//! -----END PGP PUBLIC KEY BLOCK-----"#;
15//!
16//! // Parse returns an error for invalid/incomplete data
17//! // For real usage, provide valid armored PGP data
18//! ```
19//!
20//! # Modules
21//!
22//! - [`armor`] - ASCII armor decoding (Base64 + CRC24 checksum)
23//! - [`error`] - Error types for parsing failures
24//! - [`lookup`] - Algorithm and format lookup tables
25//! - [`packet`] - Packet parsing and type definitions
26//! - [`stream`] - Binary stream reader abstraction
27
28#![warn(missing_docs)]
29
30pub mod armor;
31pub mod error;
32pub mod lookup;
33pub mod packet;
34pub mod stream;
35
36use std::sync::Arc;
37
38pub use armor::{
39 ArmorBlock, ArmorResult, MultiArmorResult, decode_armor, decode_armor_multi, looks_binary,
40};
41pub use error::{Error, Result};
42pub use packet::{Field, Packet, PacketBody};
43pub use stream::ByteStream;
44
45/// Parses ASCII-armored PGP data into a vector of packets.
46///
47/// This is the main entry point for parsing armored PGP data such as
48/// public keys, secret keys, signatures, and encrypted messages.
49///
50/// # Arguments
51///
52/// * `input` - ASCII-armored PGP data (e.g., `-----BEGIN PGP PUBLIC KEY BLOCK-----`)
53///
54/// # Errors
55///
56/// Returns an error if:
57/// - The armor format is invalid (missing headers, bad base64, checksum mismatch)
58/// - The packet structure is malformed
59///
60/// # Example
61///
62/// ```ignore
63/// let packets = gpg_inspector_lib::parse(armored_data)?;
64/// for packet in packets {
65/// println!("Packet: {} ({} bytes)", packet.tag, packet.end - packet.start);
66/// }
67/// ```
68pub fn parse(input: &str) -> Result<Vec<Packet>> {
69 let armor_result = decode_armor_multi(input)?;
70 packet::parse_packets(armor_result.bytes)
71}
72
73/// Parses raw binary PGP data into a vector of packets.
74///
75/// Use this when you have already decoded the armor or are working
76/// with raw binary PGP data.
77///
78/// # Arguments
79///
80/// * `bytes` - Raw binary PGP packet data
81///
82/// # Errors
83///
84/// Returns an error if the packet structure is malformed.
85pub fn parse_bytes(bytes: impl Into<Arc<[u8]>>) -> Result<Vec<Packet>> {
86 packet::parse_packets(bytes.into())
87}