> For the complete documentation index, see [llms.txt](https://hub.bsvblockchain.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hub.bsvblockchain.org/higher-learning/bsv-academy/bitcoin-primitives-hash-functions/walkthrough-implementation-of-sha-256-in-golang-overview/sha-256-input-and-processing.md).

# SHA-256 Input and Processing

{% embed url="<https://youtu.be/dR-wIFmAd3Q>" %}

#### Input

Hash functions are only useful if we can provide them with an **input value**.

* In our GoLang command-line implementation, the input is a **string**, which we then represent as an **array of bytes**.

Example:

```
message := []byte("abc")
fmt.Printf("Output: %b", message) 
// Output: [1100001 1100010 1100011]
```

👉 Here, the string `"abc"` is converted into its **binary representation**.

***

#### Message Block Creation

Next, we turn the input into a **message block** (or multiple blocks).

* Each block must be **448 bits long (congruent to 512 bits)**.
* To achieve this, we **pad the input** so that it’s exactly **64 bits short of a multiple of 512**.

**Padding process:**

1. Append a **single 1 bit** to the message.
2. Add **zero bits** until the total length is 64 bits short of 512.
3. Reserve the final 64 bits to store the **message length**.

Example logic in Go:

```
func AddZeroPaddingBits(messageBlock []byte, message []byte) []byte {
    zeros := make([]byte, HowMuchPadding(message))
    zeros[0] += 0x80 // add separator bit
    return append(messageBlock, zeros...)
}
```

#### Adding the Message Length

The last step in building the message block is to append the **length of the input message** (in bits).

* For `"abc"`, length = **3 bytes = 24 bits**.
* This value is appended as a **64-bit unsigned integer**.

```
func AddMessageLength(messageBlock []byte, message []byte) []byte {
    messageLength := len(message) << 3
    messageLengthInBits := make([]byte, 8)
    binary.BigEndian.PutUint64(messageLengthInBits[:], uint64(messageLength))
    return append(messageBlock, messageLengthInBits...)
}
```

👉 The result is a **padded message block**, exactly **512 bits (64 bytes)** long.

***

#### Message Schedule Creation

Once the message block is ready, SHA-256 constructs a **message schedule** of **64 words** (each 32 bits).

* The first **16 words** come directly from the message block.
* The remaining **48 words** are computed using **bitwise logical functions**.
* All sums are computed **modulo 2³²**, which is handled automatically when using `uint32` in Go.

Formula:

```
Wt = σ1(Wt-2) + Wt-7 + σ0(Wt-15) + Wt-16
```

&#x20;

Bitwise functions:

* **σ0(x) = (ROTR 7) XOR (ROTR 18) XOR (SHR 3)**
* **σ1(x) = (ROTR 17) XOR (ROTR 19) XOR (SHR 10)**

👉 ROTR = rotate right, SHR = shift right, XOR = exclusive OR.

Example Go function:

```
func BuildMessageSchedule(message []byte) [64]uint32 {
    schedule := getFirst16Words(BuildMessageBlocks(message))
    schedule = getRemaining48Words(schedule)
    return schedule
}
```

#### Example Output

After parsing `"abc"` and building the schedule:

* **First 16 words** match the message block.
* **Remaining 48 words** are generated by logical operations.

👉 This schedule is then used in the **compression step** of SHA-256.

***

✅ **Summary:**

1. **Input** string → bytes.
2. **Message block creation** → padded to 512 bits with a 64-bit length suffix.
3. **Message schedule creation** → 64 words, combining original message + derived words.

This process prepares the data for the **compression function**, where the actual SHA-256 digest is computed.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
