Skip to content

Latest commit

 

History

History
36 lines (20 loc) · 1.69 KB

sha256.md

File metadata and controls

36 lines (20 loc) · 1.69 KB

SHA256

SHA-2 (Secure Hash Algorithm 2) is a set of cryptographic hash functions. It is a signature for a data set. SHA256 algorithm generates an almost-unique, fixed 256-bit hash. It is a one way function. This makes it suitable for checking integrity of your data.

Steps in algorithm

Pre-processing

This step involves pre-processing input message to make it compatibile with the hash function. It can be divided into two main steps:

  1. Padding bits

    The total length of the message must be multiple of 512. In this step bits are appended to the end of message so that the final length of the message is 64 bits less than a multiple of 512. The formula below describes this step:

    $m + p = (n * 512) - 64$

    where:

    • $m$ - length of the message
    • $p$ - length of the padding
    • $n$ - constant value

    The first bit appended is 1 and it is followed by all 0 bits.

    Next the modulus of the original message with $2^32$ is taken to get 64 bits of data. This makes the processed message exact multiple of 512.

Initialization of the hash values and round constants

The default eight buffer values are initialized (these are hard-coded constant values representing hash values). Moreover an array k containing 64 constants is initialized.

Compression function

Pre-processed message that is $n*512$ bits long is divided into $n$ chunks of $512$ bits. Each of these chunks is then put through 64 rounds of operations where the output from each round serves as the input for the next round. During first 16 rounds 512-bit message is divided into 16 pieces, each consiting of 32 bits.

Output

After 64 rounds the whole message is hashed and it has the length of 256 bits.