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:

// 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.

Last updated