SHA-256 Implementation in Python


I wanted to implement SHA-256. I found an explanation of the algorithm in the PDF document (https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf) and implemented it based on that.

Environment

  • Python 3.10

Terminology

First, let’s review the terms used in the calculation of SHA-256.

hex digit

A 16-bit value representing 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e. A hex digit can be represented by a 4-character (4-bit) string consisting of 0 or 1, for example, 0010, 1001.

word, w-bit string
A sequence of hex digits. Examples include “abcdef” and “014d23e”. The words used in SHA calculations use big-endian format. The word size in SHA-256 is 32 bits.

Implementation Steps

While implementing from the top of the document in order may be straightforward, when adding missing parts sequentially, you may need to go back to functions implemented earlier. So, I’ll describe it here in an organized way. I focused on simplicity rather than speed or efficiency and tried to avoid using built-in functions as much as possible, so there are many areas for improvement. Following the steps below will allow you to implement a function to generate SHA-256. (The code is provided, but there isn’t much explanation of the code itself.)

  1. Implementation of Arithmetic Functions
  2. Implementation of Conversion Functions
  3. Implementation of Bit Operations
  4. SHA-256 Calculation

The entire code is also available on Gist.