CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pem

PEM file parsing in Python for extracting certificates, keys, and cryptographic objects.

Pending
Overview
Eval results
Files

core-parsing.mddocs/

Core Parsing Functions

Essential functions for parsing PEM files and strings into structured objects. These functions automatically detect and categorize different PEM object types using pattern matching.

Capabilities

String/Bytes Parsing

Parses PEM objects from string or bytes input, extracting all recognizable PEM-encoded cryptographic objects using regex patterns.

def parse(pem_str: bytes | str) -> list[AbstractPEMObject]:
    """
    Extract PEM-like objects from string or bytes.
    
    Args:
        pem_str: PEM-encoded content as bytes or string
        
    Returns:
        list[AbstractPEMObject]: List of parsed PEM objects, each typed 
        according to its content (Certificate, PrivateKey, etc.)
    """

Usage Example:

import pem

# Parse from string
pem_string = """-----BEGIN CERTIFICATE-----
MIIBkjCB/AIBATANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA7bNnbNf7hEtC...
-----END CERTIFICATE-----
-----BEGIN RSA PRIVATE KEY-----
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAO2zZ2zX+4RL...
-----END RSA PRIVATE KEY-----"""

objects = pem.parse(pem_string)
print(f"Found {len(objects)} PEM objects")

for obj in objects:
    print(f"Type: {type(obj).__name__}")
    print(f"SHA-1: {obj.sha1_hexdigest}")

# Also works with bytes
pem_bytes = pem_string.encode('utf-8')
objects_from_bytes = pem.parse(pem_bytes)

File Parsing

Reads a file and parses PEM objects from its contents using the core parse() function.

def parse_file(file_name: str | Path) -> list[AbstractPEMObject]:
    """
    Read file and parse PEM objects from it.
    
    Args:
        file_name: Path to file containing PEM objects (string or pathlib.Path)
        
    Returns:
        list[AbstractPEMObject]: List of parsed PEM objects from the file
    """

Usage Example:

import pem
from pathlib import Path

# Parse from file path string
objects = pem.parse_file("certificates.pem")

# Also works with pathlib.Path objects
cert_path = Path("certs") / "server.pem"
objects = pem.parse_file(cert_path)

# Process parsed objects
for obj in objects:
    if isinstance(obj, pem.Certificate):
        print(f"Certificate SHA-1: {obj.sha1_hexdigest}")
    elif isinstance(obj, pem.PrivateKey):
        print(f"Private key SHA-1: {obj.sha1_hexdigest}")

Parsing Details

  • Pattern Recognition: Uses regex patterns to identify PEM object boundaries and types
  • Multiple Objects: Single input can contain multiple PEM objects of different types
  • Format Support: Handles standard PEM format with -----BEGIN and -----END markers
  • Encoding: Automatically handles both UTF-8 strings and bytes input
  • Error Handling: Invalid PEM objects are skipped, valid ones are returned
  • Type Detection: Automatically instantiates appropriate object classes based on PEM headers

Install with Tessl CLI

npx tessl i tessl/pypi-pem

docs

core-parsing.md

index.md

pem-objects.md

twisted-integration.md

tile.json