# RIPEMD-160 Final Value Construction and Output

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

#### Building the Final Value

The last step of the **RIPEMD-160 hash function** is to take the **final chaining values** (the working registers after compression) and combine them into the **final 160-bit hash**.

In Go, this is handled by `PrintFinalHashValuesInHex`:

```
func PrintFinalHashValuesInHex(hashValues []uint32) string {
    finalValue := make([]byte, 20)

    binary.LittleEndian.PutUint32(finalValue[0:],  hashValues[0])
    binary.LittleEndian.PutUint32(finalValue[4:],  hashValues[1])
    binary.LittleEndian.PutUint32(finalValue[8:],  hashValues[2])
    binary.LittleEndian.PutUint32(finalValue[12:], hashValues[3])
    binary.LittleEndian.PutUint32(finalValue[16:], hashValues[4])

    return fmt.Sprintf("%x", finalValue)
}
```

👉 The five 32-bit chaining values are **concatenated in Little-Endian order**, producing a **20-byte (160-bit) result**.

***

#### Printing the Output

The result is returned to the main program and printed to **standard output**:

```
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, the program reads from **STDIN**.
* If input is provided as a **command-line argument**, it hashes that string.
* Otherwise, it prompts the user for correct usage.

***

#### Verifying the Output

We can check correctness against the **test vectors** from Dobbertin et al.’s original paper:

* Input: `"abc"`
  * Expected Output:

```
8eb208f7e05d987a9b044a8e98c6b087f15a0bfc
```

* * Actual Run:

```
./ripemd160-example abc
8eb208f7e05d987a9b044a8e98c6b087f15a0bfc
```

* Input: `"a"` repeated 1 million times
  * Expected Output:

```
52783243c1697bdbe16d37f97f68f08325dc1528
```

* * Actual Run:

```
printf a%0.s {1..1000000} | ./ripemd160-example
52783243c1697bdbe16d37f97f68f08325dc1528
```

***

✅ **Summary:**

* The **final chaining values** are concatenated in **Little-Endian order** to form the 160-bit hash.
* The result is returned as a **hex string** and printed to output.
* Verification against **published test vectors** confirms the correctness of the RIPEMD-160 implementation.


---

# 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-ripemd-160-in-golang-overview/ripemd-160-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.
