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:
Output
Explanation:
Creating SHA-256 Hash Object:
sha256.New()
creates a new SHA-256 hash object.
Writing Data to Hash:
h.Write([]byte(s))
writes the bytes of the strings
to the hash object.
Calculating Hash:
h.Sum(nil)
calculates the SHA-256 hash and returns it as a byte slice.
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