Skip to content

Instantly share code, notes, and snippets.

@girishrau
Last active January 11, 2021 15:02
Show Gist options
  • Save girishrau/c257e44f05b83325b274487c54f07a0d to your computer and use it in GitHub Desktop.
Save girishrau/c257e44f05b83325b274487c54f07a0d to your computer and use it in GitHub Desktop.
[openssl commands] Sign a file, verify a signature etc. using openssl #openssl #encryption

Sign a file using the private key

  1. Sign a file input-file.txt and base64 encode it for sharing.
$ openssl dgst -sha256 -sign ~/.ssh/id_rsa input-file.txt | base64 > signb64.txt

Alternatively

$ openssl dgst -sha256 -sign ~/.ssh/id_rsa -out sign.sha256 input-file.txt
$ openssl base64 -in sign.sha256 -out <signature>

Verify a base64 encoded signature

  1. Decode the base64 encoded signature
 $ openssl base64 -d -A -in signb64.txt -out signsha256.txt
  1. Verify the file input-file.txt against its signature
$ openssl dgst -sha256 -verify id_rsa.pub -signature signsha256.txt input-file.txt
Verified OK

Get public key in DER format

openssl rsa -in ~/.ssh/id_rsa -out id_rsa_pub.der -outform DER -pubout

Get SHA256 fingerprint from the DER format

openssl dgst -sha256 id_rsa_pub.der

Convert ssh-keygen generated private key in PEM format to PKCS8|DER formats

$ openssl pkcs8 -topk8 -inform PEM -outform PEM -in id_rsa -out id_rsa_pkcs8 -nocrypt
$ openssl pkcs8 -topk8 -inform PEM -outform DER -in id_rsa -out id_rsa_pkcs8 -nocrypt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment