# Walkthrough Implementation of SHA-256 in Golang

<figure><img src="https://hub.bsvblockchain.org/~gitbook/image?url=https%3A%2F%2F2147185133-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FWtK4ab0ntyPZBmGoKq4t%252Fuploads%252Fgit-blob-055fb62103fb8c9757cf427a939578a226471ae8%252FBSVA-HashFunctions_Ch3L1_DA1.gif%3Falt%3Dmedia&#x26;width=768&#x26;dpr=4&#x26;quality=100&#x26;sign=ab5961ce&#x26;sv=2" alt=""><figcaption></figcaption></figure>

#### Why Look at the Code?

You don’t need to know the low-level workings of SHA-256 to understand how **Bitcoin or BSV functions**. But examining an implementation is useful because many of the **concepts and operations** inside SHA-256 also appear elsewhere in BSV:

* The **XOR (exclusive OR)** bit operation is used in both **digital signatures** and **Bitcoin Script**.
* The way **binary values are combined, shifted, and mixed** shows up in transaction verification and script execution.

⚠️ **Note:** The following example is **for learning only**. It has **not been optimized or rigorously tested**. For real-world systems, always use **official, vetted libraries**.

***

#### Example Implementation in GoLang

Our simple SHA-256 implementation in Go follows three main stages:

***

**1. Input and Preprocessing**

* **Input**: Take the raw message.
* **Message Block Construction**: Break the input into fixed-size blocks.
* **Message Schedule Construction**: Expand the message into a sequence of words for use in the compression step.

***

**2. Compression**

* **Initialize Working Variables**: Set up registers with predefined constants.
* **Compute Temporary Words**: Perform logical functions, rotations, and modular additions.
* **Mutate Variables**: Update the working variables across multiple rounds of computation.
* **Integrate**: Mix the mutated values with the initial state.

***

**3. Final Value and Output**

* **Concatenate Final State**: Combine the registers after all rounds.
* **Output Digest**: Produce the **256-bit hash value**, usually expressed in hexadecimal.

***

✅ **Summary:** This walkthrough demonstrates how SHA-256 processes input into a secure digest. While the Go code itself is mainly illustrative, the underlying operations mirror those used in **digital signatures, scripts, and transaction verification** across the BSV system.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hub.bsvblockchain.org/higher-learning/bsv-academy/bitcoin-primitives-hash-functions/walkthrough-implementation-of-sha-256-in-golang-overview/walkthrough-implementation-of-sha-256-in-golang.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
