openssl dgst -sha256 -sign key.pem -out sig.der message.txtopenssl pkey -in key.pem -pubout -out pub.pemopenssl dgst -sha256 -verify pub.pem -signature sig.der message.txt# Verified OKopenssl base64 -in sig.der # base64 for JSON / emailxxd -p sig.der | tr -d "\n"; echo # hex, one lineopenssl asn1parse -inform DER -in sig.der # shows r and sHow it works
-signtakes the private key PEM (PKCS#8 or SEC1 — both work) and implies ECDSA for an EC key.- The hash flag is part of the contract: the verifier must use the same
-sha256. For P-384 keys,-sha384is the conventional pairing. - Output is raw binary DER, 70–72 bytes on P-256 — write it with
-out, never copy it from the terminal.
Gotchas
- The signature is binary: pasting it into a text field corrupts it silently. Base64- or hex-encode first (third snippet), and decode before verifying.
- DER length varies between 70 and 72 bytes on P-256 — length-validating code that expects one fixed size will reject valid signatures.
- Signing twice gives different bytes each time (random nonce). To compare signatures across runs, verify each one — never diff them.