SHA-256 Hashing
(Challenge added almost 7 years ago.)
Implement the SHA-256 Hashing Algorithm.
The Problem
Hash functions are widely used to generate small fingerprints of all kinds of data and are generally used in authentication schemes and for verifying data. MD5 and SHA-1 are currently the most commonly used hashing functions, but it would be boring to ask you to implement one of those. So, we're going for SHA-1's big brother, SHA-256.
SHA-256 works by taking arbitrary data (of maximum length 2EiB) as its input and producing, through various calculations, a 32-byte hash of that data (64 characters when represented in hex.)
I won't be able to give a better explanation of the algorithm than Wikipedia's SHA-256 entry, so I won't even try. (You'll be pleased to know that that page includes pseudocode for the algorithm which should allow you a working version fairly quickly.)
In addition, the information in the FIPS 180-2 document (PDF) may be useful since it gives worked examples (Will be useful for debugging) and is more detailed than the Wikipedia entry.
More Information
- Your code will be run five times, and on each run be given data on stdin and be expected to print the hexidecimal digest of that data on stdout.
- To make testing easier, that data will be ASCII text. Note that none of the text has trailing newlines at the end-of-file (although newlines in the text itself are significant.)
Examples
The data for the first 4 runs of your code will have the same input :
- Run 1 - Very short message.
- Run 2 - Check a 2-block message.
- Run 3 - 1,015 a's.
- Run 4 - An Aesop Fable.
The fifth and final run will be a randomised set of 28 words. To get a random sample, click below. Reload the page to get a new sample.
