PythonIntermediate · Windows, macOS, or Linux; Python 3.12+

Fingerprint a PEM certificate bundle

Calculate SHA-256 fingerprints and detect repeated certificates in a local PEM bundle.

Before you begin

  • Python 3.12 or later.
  • A local ASCII PEM certificate file, not a private key file.
What this script changesRead-only local report. No permissions, accounts, configuration, or input files are changed. Review filenames and metadata before sharing.

Set up your example

  1. Save as security-pem-fingerprints.py.
  2. Open a terminal in its folder.
  3. Run python "security-pem-fingerprints.py" --help and prepare the listed input files.

This recipe uses your own local input or creates its own example output. Follow the setup steps below.

Download the ZIP, extract it, and open a terminal inside the extracted folder. The source file is security-pem-fingerprints.py.

Run the script

Run command
python "security-pem-fingerprints.py" "certificates.pem"

If your system uses python3 instead of python, use that command for Python examples. Keep paths containing spaces in quotes. Required inputs and optional settings are described in the code and setup steps.

What to expect

Two copies of one certificate produce the same digest and the second has duplicate=true.

The complete script

security-pem-fingerprints.py
#!/usr/bin/env python3
"""Calculate SHA-256 fingerprints and detect repeated certificates in a local PEM bundle."""
import argparse
import sys
import json
from pathlib import Path
import ssl
import re
import hashlib

def main():
    parser = argparse.ArgumentParser(description='Calculate SHA-256 fingerprints and detect repeated certificates in a local PEM bundle.')
    parser.add_argument('bundle',type=Path)
    args = parser.parse_args()
    text=args.bundle.read_text(encoding='ascii')
    blocks=re.findall(r'-----BEGIN CERTIFICATE-----.*?-----END CERTIFICATE-----',text,re.S)
    if not blocks:raise ValueError('No PEM certificates found')
    seen=set()
    for index,block in enumerate(blocks,1):
        digest=hashlib.sha256(ssl.PEM_cert_to_DER_cert(block)).hexdigest()
        print(json.dumps({'certificate':index,'sha256':digest,'duplicate':digest in seen}))
        seen.add(digest)

if __name__ == '__main__':
    try:
        main()
    except (OSError, ValueError, RuntimeError) as exc:
        print(f'Error: {exc}', file=sys.stderr)
        sys.exit(1)

Open source as plain text · Download source and sample files

How it works

  1. Calculate SHA-256 fingerprints and detect repeated certificates in a local PEM bundle.
  2. Arguments are explicit and expected input errors produce a message with a nonzero exit code.
  3. Results reflect your selected local data or the specific endpoint you choose; the example illustrates the result shape.

Make it your own

Adapt the task

Supply a public certificate bundle as bundle; this computes fingerprints without validating issuers or expiry.

Adjust presentation

Edit final print statements or json.dumps indentation to adjust the report layout. Keep input validation and exclusive-create file modes intact.

Change one setting at a time, run it again on sample data, and compare the output. The customization guide walks through this process.

If something goes wrong

An input or dependency is unavailable.

A local ASCII PEM certificate file, not a private key file.

Actual results differ from the example.

Inputs and system state determine values. Review each finding in context; a report is not proof of system health or a complete security assessment.

Reference & next steps

These official references explain the underlying tools. The Scriptage example and guide are original instructional content. See verification notes for what has been checked and the limits of those checks.

Copies a template for your own notes. It does not send anything.