This is a text-only version of the following page on https://raymii.org: --- Title : Sign and verify text/files to public keys via the OpenSSL Command Line Author : Remy van Elst Date : 09-11-2015 URL : https://raymii.org/s/tutorials/Sign_and_verify_text_files_to_public_keys_via_the_OpenSSL_Command_Line.html Format : Markdown/HTML --- This small guide will shows you how to use the OpenSSL Command Line to sign a file, and how to verify the signing of this file. You can do this to prove ownership of a key, or to prove that a file hasn't been modified since you signed it. This works both with small text files as well as huge photo's, documents or PDF files.

Recently I removed all Google Ads from this site due to their invasive tracking, as well as Google Analytics. Please, if you found this content useful, consider a small donation using any of the options below:

I'm developing an open source monitoring app called Leaf Node Monitoring, for windows, linux & android. Go check it out!

Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.

You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $100 credit for 60 days.

### Generate a keypair We'll generate a new keypair for this. You can also use an exisiting one. Change the subject in the following command and execute it to generate a self signed keypair: openssl req -nodes -x509 -sha256 -newkey rsa:4096 -keyout "$(whoami)s Sign Key.key" -out "$(whoami)s Sign Key.crt" -days 365 -subj "/C=NL/ST=Zuid Holland/L=Rotterdam/O=Sparkling Network/OU=IT Dept/CN=$(whoami)s Sign Key" Also create a small text file to test the signing process on: echo "Hello, World!" > sign.txt ### Sign the file Use the following command to sign the file. We actually take the sha256 hash of the file and sign that, all in one `openssl` command: openssl dgst -sha256 -sign "$(whoami)s Sign Key.key" -out sign.txt.sha256 sign.txt This will result in a file `sign.txt` with the contents, and the file `sign.txt.sha256` with the signed hash of this file. You can place the file and the public key (`$(whoami)s Sign Key.crt`) on the internet or anywhere you like. Keep the private key (`$(whoami)s Sign Key.key`) very safe and private. ### Verify the signature To verify the signature, you need the specific certificate's public key. We can get that from the certificate using the following command: openssl x509 -in "$(whoami)s Sign Key.crt" But that is quite a burden and we have a shell that can automate this away for us. The below command validates the file using the hashed signature: openssl dgst -sha256 -verify <(openssl x509 -in "$(whoami)s Sign Key.crt" -pubkey -noout) -signature sign.txt.sha256 sign.txt If the contents have not changed since the signing was done, the output is like below: Verified OK If the validation failed, that means the file hash doesn't correspond to the signed hash. The file has very likely been modified or tampered. The result of a failed validation looks like below: Verification Failure ### Signature To get a text version of the signature (the file contains binary content) you can use the `base64` command. The textual version is easier to public online with the file: base64 sign.txt.sha256 > sign.txt.sha256.txt To get this back into `openssl` parsable output, use the `base64 -d` command: base64 -d sign.txt.sha256.txt > sign.txt.sha256 [1]: https://www.digitalocean.com/?refcode=7435ae6b8212 --- License: All the text on this website is free as in freedom unless stated otherwise. This means you can use it in any way you want, you can copy it, change it the way you like and republish it, as long as you release the (modified) content under the same license to give others the same freedoms you've got and place my name and a link to this site with the article as source. This site uses Google Analytics for statistics and Google Adwords for advertisements. You are tracked and Google knows everything about you. Use an adblocker like ublock-origin if you don't want it. All the code on this website is licensed under the GNU GPL v3 license unless already licensed under a license which does not allows this form of licensing or if another license is stated on that page / in that software: This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Just to be clear, the information on this website is for meant for educational purposes and you use it at your own risk. I do not take responsibility if you screw something up. Use common sense, do not 'rm -rf /' as root for example. If you have any questions then do not hesitate to contact me. See https://raymii.org/s/static/About.html for details.