> For the complete documentation index, see [llms.txt](https://ineelhere.gitbook.io/code-with-go/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ineelhere.gitbook.io/code-with-go/59-sha256-hashes.md).

# SHA256 Hashes

This Go code demonstrates how to use the `crypto/sha256` package to calculate the SHA-256 hash of a string. Let's go through the code with inline comments and explanations:

```go
// Importing necessary packages.
import (
	"crypto/sha256"
	"fmt"
)

// The main function, where the execution of the program begins.
func main() {
	// The string to be hashed.
	s := "sha256 this string"

	// Creating a new SHA-256 hash object.
	h := sha256.New()

	// Writing the bytes of the string to the hash object.
	h.Write([]byte(s))

	// Calculating the SHA-256 hash and getting the hash in bytes.
	bs := h.Sum(nil)

	// Printing the original string and the hexadecimal representation of the hash.
	fmt.Println(s)
	fmt.Printf("%x\n", bs)
}
```

#### Output

```
sha256 this string
1af1dfa857bf1d8814fe1af8983c18080019922e557f15a8a0d3db739d77aacb
```

Explanation:

1. **Creating SHA-256 Hash Object:**
   * `sha256.New()` creates a new SHA-256 hash object.
2. **Writing Data to Hash:**
   * `h.Write([]byte(s))` writes the bytes of the string `s` to the hash object.
3. **Calculating Hash:**
   * `h.Sum(nil)` calculates the SHA-256 hash and returns it as a byte slice.
4. **Printing Results:**
   * `fmt.Println(s)` prints the original string.
   * `fmt.Printf("%x\n", bs)` prints the hexadecimal representation of the SHA-256 hash.

This code demonstrates a basic example of using SHA-256 hashing in Go. Hashing is commonly used for securely storing passwords, generating unique identifiers, and ensuring data integrity.


---

# 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 dynamically by asking a question.

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

```
GET https://ineelhere.gitbook.io/code-with-go/59-sha256-hashes.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
