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 -hmac signs a message with a shared secret

Openssl
~/tips/tip-openssl-dgst-hmac

printf %s hello | openssl dgst -sha256 -hmac secret

Computes an HMAC of a file or text with a secret key, as used for webhook signatures.

Webhook signatures and API request signing often come down to one thing, an HMAC. Before blaming the server, it helps to check your own idea of the result against a tool you trust.

printf %s hello | openssl dgst -sha256 -hmac secret

That prints the SHA-256 HMAC of the text hello with the key secret. On OpenSSL 3.0.13 it begins with 88aab3ede8d3adf9. The printf %s keeps a newline from being added to the message, because one extra byte gives a completely different input.

For a file, give the name at the end: openssl dgst -sha256 -hmac secret body.json. Without -hmac, the same command gives a plain SHA-256 hash with no key. For hello that is 2cf24dba5fb0a30e at the start, so the two outputs are easy to tell apart when you mix them up.

The docs say the openssl mac command is preferred over -hmac. The same HMAC with it:

printf %s hello | openssl mac -digest SHA256 -macopt key:secret HMAC

It prints the same value, but in capital letters. The -macopt option also takes hexkey: for a key written in hexadecimal.

Caveat: key: is for printable characters only, so a key that comes from random bytes needs hexkey: instead. And since the docs prefer openssl mac for this job, I’d use dgst -hmac mainly for a quick check of a webhook example, with a made-up key.

Further reading: openssl-dgst documentation, openssl-mac documentation

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 openssl pkcs12 -legacy -in old.pfx -nokeys echo 'aGVsbG8gd29ybGQ=' | openssl base64 -d | openssl s_client -connect example.com:443 -servername example.com -quiet -keylogfile keys.log All Openssl tips (34) All topics

Link