> 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-final-value-construction-and-output.md).

# SHA-256 Final Value Construction and Output

{% embed url="<https://youtu.be/1mge3GDuGOg>" %}

#### From Chaining Variables to Hash Digest

The final step in SHA-256 is to take the **chaining variable values** (produced during the compression step) and **concatenate them** into a single **hexadecimal string**.

In GoLang, this is straightforward:

```
func PrintFinalHashValuesInHex(finalHashValues []uint32) string {
    hashValue := fmt.Sprintf("%x%x%x%x%x%x%x%x",
        finalHashValues[0], finalHashValues[1], finalHashValues[2], finalHashValues[3],
        finalHashValues[4], finalHashValues[5], finalHashValues[6], finalHashValues[7])
    return hashValue
}
```

👉 **Key takeaway:** The eight 32-bit outputs are joined to form the **256-bit SHA-256 digest**.

***

#### Printing the Result

Since our implementation is a **CLI program**, the final hash can be printed to **STDOUT**:

```
func main() {
    fi, _ := os.Stdin.Stat()

    if (fi.Mode() & os.ModeCharDevice) == 0 {
        reader := bufio.NewReader(os.Stdin)
        input, _ := reader.ReadString('\n')
        input = strings.TrimSuffix(input, "\n")
        fmt.Println(GetHash(input))
    } else {
        if len(os.Args) > 1 && len(os.Args) < 3 {
            fmt.Println(GetHash(os.Args[1]))
        } else {
            fmt.Println("Please provide a single input string")
        }
    }
}
```

&#x20;

* If input is piped in via STDIN, the program reads it and prints the hash.
* If run with an argument (e.g., `./sha256 abc`), it hashes that string.
* If no valid input is provided, it prompts the user.

***

#### Example Output

When we run the program with the test input `"abc"`, the result matches the **official output** from the **NIST FIPS 180-4 specification**:

```
"abc" → ba7816bf8f01cfea414140de5dae2223
         b00361a396177a9cb410ff61f20015ad
```

&#x20;

✅ **Summary:**

* The **final chaining variables** are concatenated into a 256-bit hexadecimal digest.
* The program can output the hash via **command-line input** or **STDIN**.
* Test results confirm correctness against the **official SHA-256 standard**.


---

# 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-final-value-construction-and-output.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.
