{"id":2701,"date":"2016-11-06T14:33:07","date_gmt":"2016-11-06T14:33:07","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=2701"},"modified":"2016-11-06T15:26:11","modified_gmt":"2016-11-06T15:26:11","slug":"sha256-hash","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/security\/open-ssl-c-library\/sha256-hash","title":{"rendered":"SHA256 hash"},"content":{"rendered":"<p>\nGenerate a hash of a string, file, whatever.\n<\/p>\n<h4>\nSalting your hash<br \/>\n<\/h4>\n<p>\nCombine your salt with what is being hashed (e.g. typically you do this with a password). &nbsp;SHA256&nbsp;and other hash generation algorithms don&#39;t take salt&nbsp;but if you add the salt to the end of what is being hashed you will get a different result, hence it is salted.\n<\/p>\n<h4>\nGenerating SHA256 Hash Of A File Or String<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\/\/#include &lt;openssl\/sha.h&gt;  (Also add the -lcrypto linker option)\r\n\r\n\/\/*******************************************\r\n\/\/*******************************************\r\n\/\/********** GENERATE SAHA256 HASH **********\r\n\/\/*******************************************\r\n\/\/*******************************************\r\n\/\/input_data\t\t\tThe data you want to generate a hash of\r\n\/\/data_length\t\t\tThe length of the data in bytes\r\n\/\/message_digest\t\tThe resulting hash.  Create as: unsigned char output_buffer[65];\t(32 byte hash for SHA256, converted to 2 chars of hex per byte + trailing null\r\nbool generate_sha256_hash (void *input_data, unsigned long data_length, char *output_buffer)\r\n{\r\n\tunsigned char message_digest[SHA256_DIGEST_LENGTH];\t\t\/\/32 bytes for SHA256\r\n\t\t\t\r\n    SHA256_CTX context;\r\n    if(!SHA256_Init(&amp;context))\r\n        return false;\r\n\r\n    if(!SHA256_Update(&amp;context, (unsigned char*)input_data, data_length))\t\t\/\/&lt;This can be called repeatedly if you have lots of data, e.g. a large file you want to hash\r\n        return false;\r\n\r\n    if(!SHA256_Final(message_digest, &amp;context))\r\n        return false;\r\n\r\n\t\/\/Convert from binary to hex string (2 characters of hex per byte)\r\n    int i = 0;\r\n    for(i = 0; i &lt; SHA256_DIGEST_LENGTH; i++)\r\n    {\r\n        sprintf((char*)output_buffer + (i * 2), &quot;%02x&quot;, message_digest[i]);\r\n    }\r\n    output_buffer[64] = 0;\r\n    return true;\r\n}\r\n\t\r\n\/* Example of using it\r\nchar sha256_hash[65];\r\nchar test_string[] = &quot;my test string&quot;;\r\n\tif(!generate_sha256_hash(test_string, 14, sha256_hash))\r\n\t{\r\n\t\t\/\/ERROR\r\n\r\n\t}\r\n*\/\r\n<\/code><\/pre>\n<h4>\nSHA512<br \/>\n<\/h4>\n<p>\nShould just be a case of renaming the functions and increasing the size of the output string buffer\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Generate a hash of a string, file, whatever. Salting your hash Combine your salt with what is being hashed (e.g. typically you do this with a password). &nbsp;SHA256&nbsp;and other hash generation algorithms don&#39;t take salt&nbsp;but if you add the salt to the end of what is being hashed you will get a different result, hence [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[136],"tags":[],"class_list":["post-2701","post","type-post","status-publish","format-standard","hentry","category-open-ssl-c-library"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2701","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=2701"}],"version-history":[{"count":10,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2701\/revisions"}],"predecessor-version":[{"id":2715,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2701\/revisions\/2715"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=2701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=2701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=2701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}