Crash Course in Digital Logic for SHA-256

Digital Logic Background

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Understanding Computer Magic

This article is intended to give you the tools to understand SHA-256 hashes even without a background in computers. 

Bits, Binary, and Hexadecimal

Computers process data in binary (0s and 1’s). Each 0 or 1 is called a ‘bit.’ Final SHA-256 hashes are made up of 256 bits. However, instead of 0s and 1s, hashes are typically written in hexadecimal form to save space. Each Hex number represents 4 binary digits.

Hexadecimal to Binary Conversion

Though it may look tedious, using binary instead of hexadecimal is key to understanding how SHA-256 works without hand waving away details. To make things look a bit nicer, binary is typically represented using black and white instead of 1 and 0 respectively.

Binary conversion example

Bit Math – Binary Addition

Bit math is a lot easier than it sounds. As you are only dealing with 0s and 1s, figuring 1+1 is trivial! It’s 10! =P

Let’s get into it. =)

Binary Addition Question

These represent 0100 and 0101 respectively. Addition for binary works just the same as for regular numbers, the only difference is when you ‘carry over’. In normal numbers, you carry over the 1 when you add 1+9. The same thing happens with Binary except you start carrying over at 2 (adding 2 black squares) instead of at 10.

Binary Addition Example

SHA-256 uses something called ‘modular math’. This is a high-level math concept that is remarkably simple in binary form. Addition ‘mod 24’ means that you do normal addition, but only care about the last 4 bits (full scale SHA-256 worries about the last 32).

Binary Modular Math Example

It doesn’t matter if you add together two 32 bit long chains. We only ever care about the last 4.

Bit Logic – Basic Functions

There are 5 concepts of digital logic that go into SHA-256. They are: AND, XOR, NOT, Shift and Circular Shift. An AND function results in a 1 only if both inputs are a 1, a XOR results in a 1 only if both inputs are different, a NOT reverses all the bits, and Shifts… well… shift.

XOR AND and NOT Gate Examples

Shifts move all the bits to the right (the last bit is dropped) and add 0 bit to the front. A Circular Shift also moves the bits to the right, but the last bit is moved to the front instead of being dropped.

Digital Logic Shift Example

And that completes the Craft-Crypto Crash Course in Digital Logic! Congrats!

Now how do we Hash

Subscribe To Our Newsletter

We Don't Spam.
This is mostly used for major Software Updates and other big news

Read More from Craft-Crypto

TradeCraft pro Basic crypto Bot overview
Craft-Crypto

Crypto Trading Journal Update #2

The Week that included invasions, gold spikes, and an upcoming executive order on crypto