Skip to main content

Python library for parsing and verifying OCMF (Open Charge Metering Format) signatures from electric vehicle charging stations

Project description

PyOCMF

PyPI version Python versions CI

Python library for parsing, validating, and verifying OCMF (Open Charge Metering Format) signatures from electric vehicle charging stations.

Note: This is an unofficial library that implements parts of the OCMF specification. It is not affiliated with or endorsed by S.A.F.E. e.V.. For official verification of charging session data, please use the Transparenzsoftware provided by S.A.F.E. e.V. This library may be incomplete or contain discrepancies from the official specification.

Try PyOCMF in your browser - No installation required! Parse and validate OCMF data locally and directly in your web browser using Pyodide.

Features

  • Parse OCMF strings into validated Python objects
  • Verify cryptographic signatures for data integrity
  • Support for ECDSA with multiple curves (secp192r1, secp256r1, secp384r1, secp521r1, brainpool variants)
  • Type-safe validation using Pydantic
  • Eichrecht compliance validation for German calibration law requirements

Installation

pip install pyocmf[all]

This installs the complete package with CLI tools and cryptographic signature verification.

Alternative installation options
# Minimal (parsing only, no CLI or crypto)
pip install pyocmf

# With CLI only
pip install pyocmf[cli]

# With crypto only
pip install pyocmf[crypto]

Quick Start

from pyocmf import OCMF

# Parse an OCMF string
ocmf_string = 'OCMF|{"FV":"1.0","GI":"KEBA_KCP30",...}|{"SD":"3045..."}'
ocmf = OCMF.from_string(ocmf_string)

# Access payload data
print(ocmf.payload.GI)  # Gateway ID: "KEBA_KCP30"
print(ocmf.payload.GS)  # Gateway serial number
print(ocmf.payload.RD)  # List of meter readings

# Verify signature (requires pyocmf[crypto])
is_valid = ocmf.verify_signature(public_key_hex)

Command Line Interface

# Validate an OCMF string
ocmf 'OCMF|{"FV":"1.0",...}|{"SD":"3045..."}'

# Validate and verify signature
ocmf 'OCMF|{...}|{...}' --public-key 3059301306072A8648CE3D...

# Validate from XML file (extracts public key automatically)
ocmf charging_session.xml

More Examples

Working with XML files

OCMF data is often distributed in XML format from CPO backends.

from pyocmf import OcmfContainer

container = OcmfContainer.from_xml("charging_session.xml")

for entry in container:
    print(f"Gateway: {entry.ocmf.payload.GI}")
    if entry.public_key:
        is_valid = entry.verify_signature()
        print(f"Signature: {'Valid' if is_valid else 'Invalid'}")
Eichrecht compliance checking

Validate German calibration law requirements for charging transactions.

from pyocmf import OCMF, check_eichrecht_transaction

ocmf_begin = OCMF.from_string(begin_string)
ocmf_end = OCMF.from_string(end_string)

issues = check_eichrecht_transaction(ocmf_begin, ocmf_end)
if not issues:
    print("Transaction is Eichrecht compliant")

Checks include meter status, error flags, time sync, cable loss compensation, transaction consistency, and user identification.

Public key metadata

Extract structured metadata from public keys per OCMF spec Table 23.

from pyocmf import PublicKey

public_key = PublicKey.from_string(public_key_hex)
print(f"Curve: {public_key.curve}")
print(f"Key Size: {public_key.key_size} bits")

# Check if key matches signature algorithm
matches = public_key.matches_signature_algorithm(ocmf.signature.SA)

Supported Signature Algorithms

PyOCMF supports all ECDSA signature algorithms defined in the OCMF specification:

  • secp192k1, secp256k1 - Koblitz curves
  • secp192r1, secp256r1, secp384r1, secp521r1 - NIST curves
  • brainpool256r1, brainpoolP256r1, brainpool384r1 - Brainpool curves
  • SHA256 and SHA512 hash functions

Error Handling

from pyocmf import OCMF, SignatureVerificationError, OcmfFormatError

try:
    ocmf = OCMF.from_string(ocmf_string)
    is_valid = ocmf.verify_signature(public_key)
except OcmfFormatError as e:
    print(f"Invalid OCMF format: {e}")
except SignatureVerificationError as e:
    print(f"Signature verification error: {e}")

Development

# Clone the repository
git clone https://github.com/paul-ww/pyocmf.git
cd pyocmf

# Install dependencies with uv
uv sync

# Run tests
uv run pytest

# Run type checking
uv run ty check src test

# Run linting
uv run ruff check .

Documentation

Full documentation including API reference is available at paul-ww.github.io/pyocmf.

License

See LICENSE file for details.

About OCMF

OCMF (Open Charge Metering Format) is a standardized format for metering data from electric vehicle charging stations. It ensures transparency and tamper-proof documentation of charging sessions, complying with legal requirements such as the EU Measuring Instruments Directive (MID) and German Eichrecht.

For more information about OCMF, visit safe-ev.de.


Vibe-engineered with Claude Code 🤖

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyocmf-0.2.3.tar.gz (23.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyocmf-0.2.3-py3-none-any.whl (36.6 kB view details)

Uploaded Python 3

File details

Details for the file pyocmf-0.2.3.tar.gz.

File metadata

  • Download URL: pyocmf-0.2.3.tar.gz
  • Upload date:
  • Size: 23.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pyocmf-0.2.3.tar.gz
Algorithm Hash digest
SHA256 284c05e39d049f8433ced8b5cdd0c9e7bbe70f4b6ac1fbd4dbc2266df2801bd2
MD5 7277f9e36627ced4d23302575dfe87b4
BLAKE2b-256 21f3a8f3bb98164b47d0bd215ce495edb52055e0e9c97a58a32e3dc1160175ae

See more details on using hashes here.

File details

Details for the file pyocmf-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: pyocmf-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 36.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pyocmf-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 06aad83558ca1cfc9cc0e5d5818db85e841a9d1214203ee82cb7c7f1ac8fa040
MD5 1d09f7b6036c8e6deeb7ed6f5177975f
BLAKE2b-256 0c6707181c3fa9b01e963a13993c40092bc8e9cc5d6971f6df6b37354fc671b6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page