# 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: 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/sha-256-final-value-construction-and-output.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.
