{"id":2664,"date":"2016-10-07T15:38:55","date_gmt":"2016-10-07T15:38:55","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=2664"},"modified":"2016-10-08T12:26:23","modified_gmt":"2016-10-08T12:26:23","slug":"signing-files-with-sha256","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/security\/signing-files-with-sha256","title":{"rendered":"Signing Files With SHA256"},"content":{"rendered":"<p>\nSHA256 is widely regarded as a good security hash that&#39;s&nbsp;still secure (some others such as MD5 etc are no longer considered secure). &nbsp;The following process lets you sign and verify files using sha256\n<\/p>\n<p>\nRaspbian comes with openssl&nbsp;already and the commands used below&nbsp;are console commands &nbsp;If you want to execute them programmatically you can&nbsp;use the approach <a href=\"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/console\/using-console-commands-in-code\">shown here<\/a>.\n<\/p>\n<p>\nWe used this excellent guide to create this page:&nbsp;<a href=\"http:\/\/www.zimuel.it\/sign-and-verify-a-file-using-openssl\/\">http:\/\/www.zimuel.it\/sign-and-verify-a-file-using-openssl\/<\/a>\n<\/p>\n<h4>\nGenerate a Key Pair<br \/>\n<\/h4>\n<p>\nYou&#39;ll need a public and private key file to be able to sign. &nbsp;Your private&nbsp;key is never distributed, you keep&nbsp;that safe. &nbsp;As a additional layer of security the command below will encrypt it with a password you&#39;ll also need to supply when using it.\n<\/p>\n<p>\nYour public key will need to be distributed for anyone \/ your application to use to be able to verify if the file you signed was signed using the corresponding secret private key file. &nbsp;This type of hashing verification works using clever maths&nbsp;where&nbsp;a public key generated from a private key&nbsp;can&nbsp;verify that a hash was originally created using the secret private key.\n<\/p>\n<pre>\r\n<code>\r\nsudo openssl genrsa -aes128 -passout pass:MY_PRIVATE_KEY_PASSWORD -out \/home\/pi\/projects\/private.pem 4096\r\nsudo openssl rsa -in \/home\/pi\/projects\/private.pem -passin pass:MY_PRIVATE_KEY_PASSWORD -pubout -out \/home\/pi\/projects\/public.pem\r\n<\/code><\/pre>\n<p>\nReplace MY_PRIVATE_KEY_PASSWORD with your own password that will be required whenever you need to use the private key file. &nbsp;Don&#39;t add quotation marks around it, just provide a string of characters for it to use, e.g.&nbsp;pass:abcd1234z&nbsp;-out&nbsp;\n<\/p>\n<p>\nIn your &quot;\/home\/pi\/projects\/&quot; folder you will now find the following files:\n<\/p>\n<p style=\"margin-left: 40px;\">\n\/home\/pi\/projects\/private.pem<br \/>\n\/home\/pi\/projects\/public.pem\n<\/p>\n<p>\nThese are your key files.\n<\/p>\n<h4>\nGenerate A Hash For A File<br \/>\n<\/h4>\n<p>\nThese commands will generate a hash signature file:\n<\/p>\n<pre>\r\n<code>\r\nsudo openssl dgst -sha256 -sign \/home\/pi\/projects\/private.pem -out \/tmp\/sign.sha256 \/home\/pi\/projects\/my_file_to_be_signed.a\r\nsudo openssl base64 -in \/tmp\/sign.sha256 -out \/home\/pi\/projects\/my_digital_signature_output_file.txt\r\n<\/code><\/pre>\n<p>\nThe&nbsp;file &quot;\/home\/pi\/projects\/my_digital_signature_output_file.txt&quot; can now be distributed along with your &quot;\/home\/pi\/projects\/my_file_to_be_signed.a&quot; and be used to verify that &quot;my_file_to_be_signed.a&quot; is genuine.\n<\/p>\n<p>\nThe first command&nbsp;generates the hash, the 2nd command converts it from binary to base64&nbsp;so its suitable for a text file.\n<\/p>\n<h4>\nVerifying A Hash Of A File<br \/>\n<\/h4>\n<pre>\r\n<code>\r\nopenssl base64 -d -in \/home\/pi\/projects\/my_digital_signature_output_file.txt -out \/tmp\/sign.sha256\r\nopenssl dgst -sha256 -verify \/home\/pi\/projects\/public.pem -signature \/tmp\/sign.sha256 \/home\/pi\/projects\/my_file_to_be_signed.a\r\n<\/code><\/pre>\n<p>\nIf it was successful you will get &quot;Verified OK&quot; response.\n<\/p>\n<p>\nThe first command converts the base64&nbsp;hash&nbsp;back to binary and the&nbsp;2nd command verifies&nbsp;the hash was generated from the same file using the private key.\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SHA256 is widely regarded as a good security hash that&#39;s&nbsp;still secure (some others such as MD5 etc are no longer considered secure). &nbsp;The following process lets you sign and verify files using sha256 Raspbian comes with openssl&nbsp;already and the commands used below&nbsp;are console commands &nbsp;If you want to execute them programmatically you can&nbsp;use the approach [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[134],"tags":[],"class_list":["post-2664","post","type-post","status-publish","format-standard","hentry","category-security"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2664","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=2664"}],"version-history":[{"count":7,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2664\/revisions"}],"predecessor-version":[{"id":2672,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2664\/revisions\/2672"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=2664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=2664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=2664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}