Before you begin
- Python 3.12 or later.
- A plain OpenSSH public key file. Never provide a private key.
Set up your example
- Save as security-ssh-public-key-fingerprints.py.
- Open a terminal in its folder.
- Run python "security-ssh-public-key-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-ssh-public-key-fingerprints.py.
Run the script
python "security-ssh-public-key-fingerprints.py" "id_ed25519.pub"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
Each plain public-key line produces a SHA256 fingerprint without its comment.
The complete script
#!/usr/bin/env python3
"""Print SHA-256 fingerprints for OpenSSH public key lines without showing comments or key bytes."""
import argparse
import sys
import json
from pathlib import Path
import base64
import hashlib
def main():
parser = argparse.ArgumentParser(description='Print SHA-256 fingerprints for OpenSSH public key lines without showing comments or key bytes.')
parser.add_argument('public_keys',type=Path)
args = parser.parse_args()
for number,line in enumerate(args.public_keys.read_text(encoding='utf-8').splitlines(),1):
parts=line.strip().split()
if not parts or parts[0].startswith('#'):continue
if len(parts)<2 or not parts[0].startswith(('ssh-','ecdsa-','sk-')):raise ValueError(f'Line {number}: expected plain OpenSSH public-key format')
blob=base64.b64decode(parts[1],validate=True)
digest=base64.b64encode(hashlib.sha256(blob).digest()).decode().rstrip('=')
print(json.dumps({'line':number,'type':parts[0],'fingerprint':'SHA256:'+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
- Print SHA-256 fingerprints for OpenSSH public key lines without showing comments or key bytes.
- Arguments are explicit and expected input errors produce a message with a nonzero exit code.
- 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
Use a .pub file or plain one-key-per-line file. authorized_keys options are not accepted by this parser.
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 plain OpenSSH public key file. Never provide a private key.
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.