brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: openssl dgst -sign and -verify sign a file with your own key

Openssl
~/tips/tip-openssl-dgst-sign-verify

openssl dgst -sha256 -sign private.pem -out backup.tar.gz.sig backup.tar.gz

Prove a file came from you and was not changed, using a key pair and two commands.

A checksum tells you a file arrived intact, but anyone can make a new checksum for a changed file. A signature ties the file to a private key that only you have.

Sign with the private key, then share the file, the signature and your public key:

openssl dgst -sha256 -sign private.pem -out backup.tar.gz.sig backup.tar.gz
openssl pkey -in private.pem -pubout -out public.pem

Whoever receives it checks the signature against the public key:

openssl dgst -sha256 -verify public.pem -signature backup.tar.gz.sig backup.tar.gz

A good match prints Verified OK and exits with 0. If even one byte of the file changed, or the signature was made with another key, you get Verification failure and exit code 1, which a script can act on.

I think this is a neat fit for a homelab: sign a config bundle or a script on one machine, and only run it on another one after the check passes. An EC key from genpkey works well and keeps the signature small. With an Ed25519 key, leave out -sha256: EdDSA does its own hashing, and OpenSSL 3 refuses an explicit digest with an error.

The check is only as good as the public key you compare with. If someone can swap both the file and public.pem, the check passes on their file. Get the public key to the other side once, over a channel you trust, and keep it there.

Further reading: OpenSSL: openssl-dgst, OpenSSL: openssl-pkeyutl

More Openssl tips openssl s_client -connect 203.0.113.10:443 -servername example.com </dev/null | openssl x509 -noout -subject openssl x509 -in cert.pem -days 365 -signkey key.pem -out cert-new.pem printf %s hello | openssl dgst -sha256 -hmac secret openssl pkcs12 -legacy -in old.pfx -nokeys echo 'aGVsbG8gd29ybGQ=' | openssl base64 -d All Openssl tips (34) All topics

Link