SHA-256 algorithm
Step 1: Convert the message into bits using ASCII table
Step 2: Add a 1 to the end of the converted message, then add zeros until it reach 448 mod 512 bits
long
Step 3: Convert the length of the initial message to a 64-bit value, then added to the end of the
padded message to create a multiply-of-512-bit-long message.
Step 4: Initialize hash value (H)
Initialize 8 hash value (h0 - h7) based on the fractional part of square root of the first 8 prime numbers:
2, 3, 5, 7, 11, 13, 17, 19
Example: Squareroot of 2 = 0.4142356237 x 232 = 1779033703 (DEC) = 6a09e667 (HEX) = h0
h0 = 6a09e667 h4 = 510e527f
h1 = bb67ae85 h5 = 9b05688c
h2 = 3c6ef372 h6 = 1f83d9ab
h3 = a54ff53a h7 = 5be0cd19
Step 5: Initialize constants (K)
Initialize 64 constants (k0 - k63) based on the fractional part of cube root of the first 64 prime numbers
(2, 3, 5, …, 311)
Example: Cube root of 2 = 0.25992104989 x 232 = 1116352408 (DEC) = 428A2F98 (HEX) = k0
k0 = 428a2f98 k16 = e49b69c1 k32 = 27b70a85 k48 = 19a4c116
k1 = 71374491 k17 = efbe4786 k33 = 2e1b2138 k49 = 1e376c08
k2 = b5c0fbcf k18 = 0fc19dc6 k34 = 4d2c6dfc k50 = 2748774c
k3 = e9b5dba5 k19 = 240ca1cc k35 = 53380d13 k51 = 34b0bcb5
k4 = 3956c25b k20 = 2de92c6f k36 = 650a7354 k52 = 391c0cb3
k5 = 59f111f1 k21 = 4a7484aa k37 = 766a0abb k53 = 4ed8aa4a
k6 = 923f82a4 k22 = 5cb0a9dc k38 = 81c2c92e k54 = 5b9cca4f
k7 = ab1c5ed5 k23 = 76f988da k39 = 92722c85 k55 = 682e6ff3
k8 = d807aa98 k24 = 983e5152 k40 = a2bfe8a1 k56 = 748f82ee
k9 = 12835b01 k25 = a831c66d k41 = a81a664b k57 = 78a5636f
k10 = 243185be k26 = b00327c8 k42 = c24b8b70 k58 = 84c87814
k11 = 550c7dc3 k27 = bf597fc7 k43 = c76c51a3 k59 = 8cc70208
k12 = 72be5d74 k28 = c6e00bf3 k44 = d192e819 k60 = 90befffa
k13 = 80deb1fe k29 = d5a79147 k45 = d6990624 k61 = a4506ceb
k14 = 9bdc06a7 k30 = 06ca6351 k46 = f40e3585 k62 = bef9a3f7
k15 = c19bf174 k31 = 14292967 k47 = 106aa070 k63 = c67178f2
Step 6: Create message schedule (w)
The 512-bit message will be divided to 16 32-bit blocks. Then we will add 0 to the end so that the input
data will become a 64 32-bit blocks (This means adding 48 32-bit blocks of 0). This become an array
w[0…63]
Each added block containing only 0 will then be modify using the following algorithm:
s0 = (w[i - 15] rightrotate 7) ⊕ (w[i - 15] rightrotate 18) ⊕ (w[i - 15] rightshift 3)
s1 = (w[i - 2] rightrotate 17) ⊕ (w[i - 2] rightrotate 19) ⊕ (w[i - 2] rightshift 10)
w[i] = w[i - 16] + s0 + w[i - 7] + s1
with i runs from 16 to 63 (total of 48 times changing w[16] to w[63])
Step 7: Compression
Initialize 8 variables: a, b, c, d, e, f, g, h and set them to be h0, h1, h2, h3, h4, h5, h6, h7 respectively.
The compression process is describe as follow:
Ch(E, F, G) = (E ∧ F) ⊕ (¬E ∧ G)
Ma(A, B, C) = (A ∧ B) ⊕ (A ∧ C) ⊕ (B ∧ C)
∑0 (A) = (A rightrotate 2) ⊕ (A rightrotate 13) ⊕ (A rightrotate 22)
∑1 (E) = (E rightrotate 6) ⊕ (E rightrotate 11) ⊕ (E rightrotate 25)
1. Compute (w0 + k0) mod 232 (Called R1)
2. Compute (Ch(E, F, G) + H + R1) mod 232 (Called R2)
3. Compute (∑1 (E) + R2) mod 232 (Called R3)
4. Compute (D + R3) mod 232 (Called R4) and (R2 + Ma(A, B, C)) mod 232 (Called R5)
5. Compute (R5 + ∑0 (A)) mod 232 (Called R6)
6. Transmute a, b, c, d, e, f, g, h as: h = g, g = f, f = e, e = R4, d = c, c = b, b = a, a = R6
This process is repeated 64 times. At the end of the 64th process, 8 variables a to h will combine with h-
to h7, respectively, to calculate modular addition.
h0 = (h0 + a) mod 232
h1 = (h1 + b) mod 232
h2 = (h2 + c) mod 232
h3 = (h3 + d) mod 232
h4 = (h4 + e) mod 232
h5 = (h5 + f) mod 232
h6 = (h6 + g) mod 232
h7 = (h7 + h) mod 232
Then h0 to h7 will be converted to hexadecimal. The hash value will be h0h1h2h3h4h5h6h7 (Combine
8 string created from converted h0-h7)