This code is intended as a demo and should not be used in production as-is.
Sha is Single Hash Algorithm and is a reliable one-way hashinh algorithm that is used to create cryptographic specific lenght texts from a list of characters. This demo presents the algorithm for the SHA512 algo (there's a list of supported SHA algorithms including, SHA1|SHA2).
This SHA512 algorithm takes as input a message with maximum length, L
less than 2128 amd produces an output of 512-bit digest. The input passed is processes in different blocks, each 1024-bit long.
The message (to be hashed) is padded, so that L == 896 mod 1024
, with single 1-bit, followed by as many 0-bit as needed.
The SHA ritual consists of some prerequisite data that need to be provided. Think of them as ritual ingredient without which, the algorithm might be lacking.
-
First 8 prime numbers: these sets of integers from 2-19 are used as a buffer (state) to hold the results of the SHA algorithm at every block computation. We will understand why there are various block computations. These prime numbers are computed to the first 64-bit of the fractional parts of their square roots.
-
80 SHA Constants: These are used in the SHA512 hashing rounds as additive constants. They are derived from using the first 64-bit of the fractional parts of the cube roots of the first 80 prime integers. It's usually referred to as the message schedule.
SHA512 is made up of 80 rounds of hashing that the code will depict. For each round we will use the buffer defined and also the Hashing constants defined to compute the digest. The digest at the end of the 80th round is also computed to give the final output digest. This 80 round hashing is done for each (1024bit) block of the message input.
Refer Chapter 11 of William Stallings - Cryptography and Network Security Principles and Practice, Global Edition-Pearson (2022) book.